java并发编程(内部静态类,ThreadLocal,MyQueue,Synchronized(lock),CountDownLatch)

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();
	}
}

希望以上内容分享能够帮助到您!

发布了67 篇原创文章 · 获赞 62 · 访问量 6951

猜你喜欢

转载自blog.csdn.net/Asia1752/article/details/104232130
今日推荐