码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA并发编程2

时间:2018-04-22 22:40:17      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:存在   写入   object   pre   rgs   col   ide   ons   AC   

说明:先上代码,笔记后续补充
public class CoderTest1 {

private static Object key;

public static void main(String[] args) {

//同步容器 Vector HashTable Collections.synchronizedList()
//如果迭代过程中发生修改,并发修改异常 修改线程独立copy一份集合(这样会导致不能得到最新的数据,效率有影响)
/*多个线程操作容易抛出ArrayIndexOutOfBoundsException异常
* Vector<Object> vector = new Vector<>();
int lastSize = vector.size()-1;
vector.remove(lastSize);
* */
// Vector<Object> vector = new Vector<>();
// synchronized (key){
// int lastSize = vector.size()-1;
// vector.remove(lastSize);
// }
//这种情况会出现并发修改异常 ConcurrentModificationException
// ArrayList<Object> list = new ArrayList<>();
// list.add(1);
// list.add(1);
// list.add(1);
// list.add(1);
// list.add(1);
// for (Object obj : list){
// System.out.println(obj);
// list.remove(obj);
// }

//并发容器 ConcurrentHashMap 分段锁 每一个链表上一个锁,1.8之后好像是node锁
//map.putIfAbsent 只有key不存在才会操作成功,不会覆盖
//CopyOnWriteArrayList/Set 应用场景:读操作多,写操作很少
//阻塞队列
// 先进先出,如果队列为空则阻塞
// ConcurrentHashMap map = new ConcurrentHashMap();
// map.putIfAbsent(1,2);
// map.putIfAbsent(1,3);
// System.out.println(map);
//
// CopyOnWriteArrayList<Object> list = new CopyOnWriteArrayList<>();
// BlockingQueue queue = new ArrayBlockingQueue(2);

// 1闭锁 2栅栏 3信号量
//到达栅栏之后需要所有线程执行完之后才能继续执行其他线程
// final CountDownLatch latch = new CountDownLatch(2);
// //第一个线程
// new Thread(){
// public void run(){
// try {
// System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");
// Thread.sleep(3000);
// System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");
// latch.countDown();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }.start();
// //第二个线程
// new Thread(){
// public void run(){
// try {
// System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");
// Thread.sleep(3000);
// System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");
// latch.countDown();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }.start();
//
// try {
// System.out.println("等待两个子线程执行完毕。。。");
// //闭锁
// latch.await();
// System.out.println("两个子线程已经执行完毕。。。");
// System.out.println("继续执行主线程");
// } catch (InterruptedException e) {
// e.printStackTrace();
// }

int N = 4;
CyclicBarrier barrier = new CyclicBarrier(N);
for (int i=0; i<N; i++){
new Writer(barrier).start();
}
int workNum = 8;//工人数目
Semaphore semaphore = new Semaphore(5);//信号量,机器数目
for (int i=0; i<workNum; i++){
new Worker(i,semaphore).start();
}
}

//用于栅栏
static class Writer extends Thread{
private CyclicBarrier cyclicBarrier;
public Writer(CyclicBarrier cyclicBarrier){
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run(){
System.out.println("线程"+Thread.currentThread().getName()+"正在写入数据。。。");
try {
Thread.sleep(5000);
System.out.println("线程"+Thread.currentThread().getName()+"写入数据完毕,等待其他线程写入数据。。。");
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("所有线程写入数据完毕,继续执行其他任务。。。");
}
}

//用于信号量
static class Worker extends Thread{
private int num;
private Semaphore semaphore;
public Worker(int num, Semaphore semaphore){
this.num = num;
this.semaphore = semaphore;
}
@Override
public void run(){
try {
semaphore.acquire();
System.out.println("工人"+this.num+"占用一个机器生产。。。");
Thread.sleep(3000);
System.out.println("工人"+this.num+"释放出机器");
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

JAVA并发编程2

标签:存在   写入   object   pre   rgs   col   ide   ons   AC   

原文地址:https://www.cnblogs.com/sleepy-goblin/p/8910023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!