一. 实现两个线程。轮流打印出数字。例如以下:

bThread --> 10
aThread --> 9
bThread --> 8
aThread --> 7
bThread --> 6
aThread --> 5
bThread --> 4
aThread --> 3
bThread --> 2
aThread --> 1

用java中的Lock类实现:

package com.yjq.thread_demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class TwoThreadPrinter { private Lock threadLock = new ReentrantLock(); private boolean flag = false; int count =10; Thread aThread = new Thread(new Runnable() {
public void run() {
while (true) {
// 锁定
threadLock.lock();
try {
if ( count < 1) {
return;
}
if (flag) {
// aThread的任务
System.out.println("aThread --> " + (count--));
flag = !flag;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
// 释放锁
threadLock.unlock();
}
}
}
}); Thread bThread = new Thread(new Runnable() {
public void run() {
while (true) {
// 锁定
threadLock.lock();
try {
if ( count < 1) {
return;
}
if (!flag) {
// aThread的任务
System.out.println("bThread --> " + (count--));
flag = !flag;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
// 释放锁
threadLock.unlock();
}
}
}
}); public void startTwoThread() {
aThread.start();
bThread.start();
} public static void main(String[] args) {
TwoThreadPrinter twoThreadPrinter = new TwoThreadPrinter();
twoThreadPrinter.startTwoThread();
} }

用synchronized实现:

package com.yjq.thread_demo;

public class TwoThreadPrinter2 {

private Object threadLock = new Object();

	int count =10;

	Thread aThread = new Thread(new Runnable() {
public void run() {
while (true) {
// 锁定
synchronized (threadLock) {
if ( count < 1) {
return;
} // // aThread的任务
System.out.println("aThread --> " + (count--)); threadLock.notify();
try {
threadLock.wait();
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
}); Thread bThread = new Thread(new Runnable() {
public void run() {
while (true) {
// 锁定
synchronized (threadLock) {
if ( count < 1) {
return;
} // // aThread的任务
System.out.println("bThread --> " + (count--)); threadLock.notify();
try {
threadLock.wait();
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
}); public void startTwoThread() {
aThread.start();
bThread.start();
} public static void main(String[] args) {
TwoThreadPrinter twoThreadPrinter = new TwoThreadPrinter();
twoThreadPrinter.startTwoThread();
} }

用Lock类的方法比較easy理解。 lock() 和 unlock()之间的块是被锁定的

用synchronize的方法少用了个flag来标志轮到哪个线程来打印,这是由于线程锁的notifity( )方法会释放锁并唤醒其它线程 ,线程锁的wait( )方法则是释放锁并休眠当前线程,假设刚好仅仅有两个线程,那么自然就是開始还有一个线程而休眠本线程。

二.扩展到n个线程轮流打印数字的问题

n个线程轮流打印数字。用Lock类是比較easy扩展的

比方三个线程轮流打印数字

package com.myexample.test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class ThreeThreadPrinter { private Lock threadLock = new ReentrantLock(); private int flag = 0; int count =10; Thread aThread = new Thread(new Runnable() {
public void run() {
while (true) {
// 锁定
threadLock.lock();
try {
if ( count < 1) {
return;
}
if (count%3 == 0 ) {
// aThread的任务
System.out.println("aThread --> " + count);
count--;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
// 释放锁
threadLock.unlock();
}
}
}
}); Thread bThread = new Thread(new Runnable() {
public void run() {
while (true) {
// 锁定
threadLock.lock();
try {
if ( count < 1) {
return;
}
if (count%3 == 1 ) {
// aThread的任务
System.out.println("bThread --> " + count);
count--;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
// 释放锁
threadLock.unlock();
}
}
}
}); Thread cThread = new Thread(new Runnable() {
public void run() {
while (true) {
// 锁定
threadLock.lock();
try {
if ( count < 1) {
return;
}
if (count%3 == 2 ) {
// aThread的任务
System.out.println("cThread --> " + count);
count--;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
// 释放锁
threadLock.unlock();
}
}
}
}); public void startTwoThread() {
aThread.start();
bThread.start();
cThread.start();
} public static void main(String[] args) {
ThreeThreadPrinter twoThreadPrinter = new ThreeThreadPrinter();
twoThreadPrinter.startTwoThread();
} }

输出结果

bThread --> 10
aThread --> 9
cThread --> 8
bThread --> 7
aThread --> 6
cThread --> 5
bThread --> 4
aThread --> 3
cThread --> 2
bThread --> 1

最新文章

  1. 第3月第19天 cxx_destruct dispatch_get_main_queue()死锁
  2. python之消息队列
  3. C/C++中static关键字详解
  4. RANSAC随机一致性采样算法学习体会
  5. dedecms手机站要同步pc站的图片
  6. javascript框架库API入口
  7. 标头“Vary:Accept-Encoding”指定方法及其重要性分析
  8. Android二维码开源项目zxing用例简化和生成二维码、条形码
  9. python学习之文本文件上传
  10. spring 装配
  11. 如何用java语言实现C#中的ref关键字(按引用传递参数)的效果
  12. 1. Dubbo原理解析-Dubbo内核实现之SPI简单介绍 (转)
  13. Html5 手机端网页
  14. Mysql主从同步(1) - 概念和原理介绍 以及 主从/主主模式 部署记录
  15. Noi.ac #309. Mas的童年(贪心)
  16. STM32 TIMER OUTPUT DIAGRAM
  17. CentOS 6.5通过yum安装和配置MySQL
  18. Send a WhatsApp Message programatically -- Tasker WhatsTasker
  19. 执行Socket socket = new Socket(ip, port);时抛出个异常:android.os.NetworkOnMainThreadException解决办法
  20. Classical Binary Search

热门文章

  1. 零基础学python-2.24 一些经常使用函数
  2. POJ 1679 The Unique MST(推断最小生成树_Kruskal)
  3. luogu2242 公路维修问题
  4. Hdu-6230 2017CCPC-哈尔滨站 A.Palindrome Manacher 主席树
  5. Codeforces Round #512 (Div. 2) D.Vasya and Triangle 数学
  6. BZOJ 4032 trie树+各种乱搞
  7. C#操作Mysql类
  8. Aspose.Words进行Word替换(插入图片和水印)
  9. 从Android源码分析View绘制
  10. 前端-JQ思维导图