java併發編程(內部靜態類,ThreadLocal,MyQueue,Synchronized(lock),CountDownLatch)
內部靜態類
public class Singletion {
private static class InnerSingletion{
private static Singletion single=new Singletion();
}
public Singletion getInstance() {
return InnerSingletion.single;
}
}
ThreadLocal
public class MyThreadLocal {
public static ThreadLocal<String> th=new ThreadLocal<String>();
public void setTh(String value) {
th.set(value);
}
public void getTh() {
System.out.println(Thread.currentThread().getName()+" "+th.get());
}
public static void main(String[] args) {
final MyThreadLocal mt=new MyThreadLocal();
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
mt.setTh("張三");
mt.getTh();
}
},"t1");
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2);
mt.getTh();
} catch (Exception e) {
e.printStackTrace();
}
}
},"t2");
t1.start();
t2.start();
}
}
MyQueue
public class MyQueue {
private LinkedList<Object> list=new LinkedList<Object>();
private AtomicInteger count=new AtomicInteger(0);
private final int minSize=0;
private final int maxSize;
public MyQueue(int maxSize) {
this.maxSize=maxSize;
}
private final Object lock=new Object();
public void put(Object obj) {
synchronized(lock) {
while(count.get()==maxSize) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
list.add(obj);
count.incrementAndGet();
lock.notify();
System.out.println("新加入的元素爲:"+obj);
}
}
public Object take() {
Object result=null;
synchronized(lock) {
while(count.get()==minSize) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
result=list.removeFirst();
count.decrementAndGet();
lock.notify();
}
return result;
}
public int getSize() {
return list.size();
}
public static void main(String[] args) {
final MyQueue mq=new MyQueue(5);
mq.put("a");
mq.put("b");
mq.put("c");
mq.put("d");
mq.put("e");
System.out.println("當前容器的長度:"+mq.getSize());
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
mq.put("f");
mq.put("g");
}
},"t1");
t1.start();
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
Object o=mq.take();
System.out.println("移除的對象爲:"+o);
Object o1=mq.take();
System.out.println("移除的對象爲:"+o1);
}
},"t2");
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
t2.start();
}
}
Synchronized(lock)
public class ListAdd {
private volatile static List<String> list=new ArrayList<String>();
public void add() {
list.add("hao123");
}
public int size() {
return list.size();
}
public static void main(String[] args) {
final ListAdd listAdd=new ListAdd();
final Object lock=new Object();
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
try {
synchronized(lock) {
for(int i=0;i<10;i++) {
listAdd.add();
System.out.println(Thread.currentThread().getName()+"添加了一個元素...");
Thread.sleep(500);
if(i==5) {
System.out.println("已經發出通知..");
lock.notify();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
},"t1");
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
synchronized(lock) {
if(listAdd.size()!=5) {
try {
System.out.println("進入t2...");
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("當前線程:"+Thread.currentThread().getName()+" 收到通知線程停止..");
throw new RuntimeException();
}
}
},"t2");
t2.start();
t1.start();
}
}
CountDownLatch
public class ListAdd2 {
private static volatile List list=new ArrayList();
public void add() {
list.add("hao");
}
public int size() {
return list.size();
}
public static void main(String[] args) {
final ListAdd2 listAdd2=new ListAdd2();
final CountDownLatch countDownLatch=new CountDownLatch(1);
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
try {
for(int i=0;i<10;i++) {
System.out.println(Thread.currentThread().getName()+" 添加了一個元素...");
Thread.sleep(500);
if(i==5) {
System.out.println("已發出通知..");
countDownLatch.countDown();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
},"t1");
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("進入t2");
countDownLatch.await();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("當前線程"+Thread.currentThread().getName()+"收到線程通知,停止...");
throw new RuntimeException();
}
},"t2");
t2.start();
t1.start();
}
}
完
希望以上內容分享能夠幫助到您!