国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ReentrantLock

2019-11-11 01:48:53
字體:
來源:轉載
供稿:網友
import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 一道經典的Java多線程編程題 * Created by yuanyong on 17/2/7. * 問題描述 啟動3個線程打印遞增的數字, 線程1先打印1,2,3,4,5, 然后是線程2打印6,7,8,9,10, 然后是線程3打印11,12,13,14,15. 接著再由線程1打印16,17,18,19,20....以此類推, 直到打印到75. 程序的輸出結果應該為: */public class ThreadDemo2 { final Lock lock = new ReentrantLock(); final Condition condition_1 = lock.newCondition(); final Condition condition_2 = lock.newCondition(); final Condition condition_3 = lock.newCondition(); public static volatile int number = 0; class RunClass implements Runnable { ConcurrentLinkedQueue<Integer> stack; RunClass(Integer id, ConcurrentLinkedQueue<Integer> stack) { this.id = id; this.stack = stack; } PRivate int id; public int getId() { return id; } public void setId(int id) { this.id = id; } private void wirte() { int i = 5; while (i > 0) { number++; System.out.println(Thread.currentThread().getName() + ":" + number); i--; } } @Override public void run() { switch (id) { case 1: while (stack.size() > 0) { //第一次 if (number == 0) { lock.lock(); try { wirte(); stack.poll(); condition_2.signal(); } finally { lock.unlock(); } } else { lock.lock(); try { condition_1.await(); if (stack.poll() == number) { wirte(); condition_2.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } break; case 2: while (stack.size() > 0) { lock.lock(); try { condition_2.await(); if (stack.poll() == number) { wirte(); condition_3.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; case 3: while (stack.size() > 0) { lock.lock(); try { condition_3.await(); if (stack.poll() == number) { wirte(); condition_1.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; } } } public static void main(String args[]) { ConcurrentLinkedQueue<Integer> stack1 = new ConcurrentLinkedQueue<Integer>(); ConcurrentLinkedQueue<Integer> stack2 = new ConcurrentLinkedQueue<Integer>(); ConcurrentLinkedQueue<Integer> stack3 = new ConcurrentLinkedQueue<Integer>(); ThreadDemo2 threadDemo2 = new ThreadDemo2(); RunClass runClass = threadDemo2.new RunClass(1, stack1); RunClass runClass2 = threadDemo2.new RunClass(2, stack2); RunClass runClass3 = threadDemo2.new RunClass(3, stack3); Thread thread1 = new Thread(runClass, "線程1"); Thread thread2 = new Thread(runClass2, "線程2"); Thread thread3 = new Thread(runClass3, "線程3"); int n_1 = 0; int n_2 = 5; int n_3 = 10; for (int n = 0; n < 5; n++) { stack1.add(n_1); stack2.add(n_2); stack3.add(n_3); n_1 += 15; n_2 += 15; n_3 += 15; } thread3.start(); thread2.start(); thread1.start(); }}

實例2 線程屏障

CountDownLatch單詞使用

import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 一道經典的Java多線程編程題 * Created by yuanyong on 17/2/7. * 問題描述 啟動3個線程打印遞增的數字, 線程1先打印1,2,3,4,5, 然后是線程2打印6,7,8,9,10, 然后是線程3打印11,12,13,14,15. 接著再由線程1打印16,17,18,19,20....以此類推, 直到打印到75. 程序的輸出結果應該為: */public class ThreadDemo2 { ConcurrentLinkedQueue<Integer> stack1 = new ConcurrentLinkedQueue<Integer>(); ConcurrentLinkedQueue<Integer> stack2 = new ConcurrentLinkedQueue<Integer>(); ConcurrentLinkedQueue<Integer> stack3 = new ConcurrentLinkedQueue<Integer>(); final CountDownLatch startGate=new CountDownLatch(1);// final CountDownLatch endGate=new CountDownLatch(3); final Lock lock = new ReentrantLock(); final Condition condition_1 = lock.newCondition(); final Condition condition_2 = lock.newCondition(); final Condition condition_3 = lock.newCondition(); public static volatile int number = 0; class RunClass2 implements Runnable{ public void run() { //第一次 if (number == 0) { lock.lock(); try { wirte(); stack1.poll(); condition_2.signal(); } finally { lock.unlock(); } } } } private void wirte() { int i = 5; while (i > 0) { number++; System.out.println(Thread.currentThread().getName() + ":" + number); i--; } } class RunClass implements Runnable { ConcurrentLinkedQueue<Integer> stack; RunClass(Integer id, ConcurrentLinkedQueue<Integer> stack) { this.id = id; this.stack = stack; } private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public void run() {// endGate.countDown();// System.out.println(endGate.getCount()); try { startGate.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("switch--------"); switch (id) { case 1: while (stack.size() > 0) { lock.lock(); try { condition_1.await(); if (stack.poll() == number) { wirte(); condition_2.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; case 2: while (stack.size() > 0) { lock.lock(); try { condition_2.await(); if (stack.poll() == number) { wirte(); condition_3.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; case 3: while (stack.size() > 0) { lock.lock(); try { condition_3.await(); if (stack.poll() == number) { wirte(); condition_1.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; } } public ConcurrentLinkedQueue<Integer> getStack() { return stack; } public void setStack(ConcurrentLinkedQueue<Integer> stack) { this.stack = stack; } } public static void main(String args[]) throws BrokenBarrierException, InterruptedException { ThreadDemo2 threadDemo2 = new ThreadDemo2(); RunClass runClass = threadDemo2.new RunClass(1, threadDemo2.stack1); RunClass runClass2 = threadDemo2.new RunClass(2, threadDemo2.stack2); RunClass runClass3 = threadDemo2.new RunClass(3, threadDemo2.stack3); Thread thread1 = new Thread(runClass, "線程1"); Thread thread2 = new Thread(runClass2, "線程2"); Thread thread3 = new Thread(runClass3, "線程3"); int n_1 = 0; int n_2 = 5; int n_3 = 10; for (int n = 0; n < 5; n++) { threadDemo2.stack1.add(n_1); threadDemo2.stack2.add(n_2); threadDemo2.stack3.add(n_3); n_1 += 15; n_2 += 15; n_3 += 15; }// threadDemo2.endGate.wait(); thread1.start(); thread2.start(); thread3.start(); Thread.sleep(3000); threadDemo2.startGate.countDown(); new Thread(threadDemo2.new RunClass2()).start(); }}

CyclicBarrier可以重復使用對應博文地址: http://blog.csdn.net/shihuacai/article/details/8856407


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台东县| 崇州市| 蓝山县| 浙江省| 淳化县| 深水埗区| 黑龙江省| 辉南县| 诸城市| 九龙城区| 涡阳县| 志丹县| 秦皇岛市| 乌兰浩特市| 怀来县| 龙泉市| 蓬莱市| 宁武县| 易门县| 南平市| 钟山县| 丰镇市| 永川市| 静乐县| 张家港市| 邵东县| 聂荣县| 娱乐| 泰和县| 桂阳县| 江源县| 固原市| 时尚| 卢氏县| 砀山县| 紫云| 涟源市| 富蕴县| 诏安县| 乌拉特后旗| 澄城县|