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

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

Java n个线程轮流打印数字的问题的更多相关文章

  1. java启动3个线程轮流打印数字

    转自:http://blog.csdn.net/u014011112/article/details/50988769 http://blog.csdn.net/perrywork/article/d ...

  2. Java多个线程顺序打印数字

    要求 启动N个线程, 这N个线程要不间断按顺序打印数字1-N. 将问题简化为3个线程无限循环打印1到3 方法一: 使用synchronized 三个线程无序竞争同步锁, 如果遇上的是自己的数字, 就打 ...

  3. 如何控制Java中的线程,总结了3种方法...

    问题:利用Java多线程,轮流打印数字,也就是怎么控制线程.... 1:通过synchronized的关键字,对类的static final 成员进行Lock,锁住对象,来实现同步. private ...

  4. 使用Java线程并发库实现两个线程交替打印的线程题

    背景:是这样的今天在地铁上浏览了以下网页,看到网上一朋友问了一个多线程的问题.晚上闲着没事就决定把它实现出来. 题目: 1.开启两个线程,一个线程打印A-Z,两一个线程打印1-52的数据. 2.实现交 ...

  5. 使用Java实现三个线程交替打印0-74

    使用Java实现三个线程交替打印0-74 题目分析 三个线程交替打印,即3个线程是按顺序执行的.一个线程执行完之后,唤醒下一个线程,然后阻塞,等待被该线程的上一个线程唤醒.执行的顺序是一个环装的队列 ...

  6. 使用Java 多线程编程 让三个线程轮流输出ABC,循环10次后结束

    简要分析: 要求三个线程轮流输出,这里我们要使用一个对象锁,让关键部分的代码放入同步块当中.同时要有一个变量记录打印的次数到达10次循环后不再打印,另外一个就是要给每个线程一个标志号,我们根据标识号来 ...

  7. Java线程同步打印ABC

    需求: 三个线程,依次打印ABCABCABC.... 方案一: 使用阻塞队列,线程1从队列1获取内容打印,线程2从队列2获取内容打印,线程3从队列3中获取内容打印.线程1把B放到队列3中,线程2把C放 ...

  8. java实现第七届蓝桥杯打印数字

    打印数字 打印数字 小明写了一个有趣的程序,给定一串数字. 它可以输出这串数字拼出放大的自己的样子. 比如"2016"会输出为: 00000 1 6666 2 0 0 1 1 6 ...

  9. Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor

    介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...

随机推荐

  1. luogu3807 【模板】 卢卡斯定理

    题目大意 对于一个很大的$n,m,p$如何求$C_{n+m}^m\mod p$? Lucas定理 若$n_i,m_i$分别是$n,m$在$p$进制下第$i$位的数字,则有 $$C_n^m\mod p= ...

  2. hdoj--1829--A Bug's Life(带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  3. 【POJ 1084】 Square Destroyer

    [题目链接] http://poj.org/problem?id=1084 [算法] 迭代加深 [代码] #include <algorithm> #include <bitset& ...

  4. 一致性hash 算法 (转)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  5. [Plugin] JQuery.uploadify上传文件插件的使用详解For ASP.NET

    URL:http://www.cnblogs.com/xiaopin/archive/2010/01/21/1653523.html 今天下午整理文件上传的例子,感觉收集到的例子都很不人性话,后来找到 ...

  6. C - Unary(map)

    Problem description Unary is a minimalistic Brainfuck dialect in which programs are written using on ...

  7. ADO.NET改进防注入

    static void Main1(string[] args) { //用户输入一个需要查询的条件 car表 Console.WriteLine("请输入"); string c ...

  8. Splay树

    class SplayNode { public: SplayNode *child[]; char value; int size; bool flip; SplayNode(), flip(fal ...

  9. mybatis学习笔记之学习目录(1)

    mybatis学习笔记之学习结构(1) 学习结构: 1.mybatis开发方法 原始dao开发方法(程序需要编写dao接口和dao实现类) mybatis的mapper接口(相当于dao接口)代理开发 ...

  10. 当接口上配了 FeignClient 和 RequestMapping 两个注解,结果错误提示 重复mapping处理方法

    再接手老文档的时候,发现有这么一个问题 错误显示为: 原文档写法: 解决方法: 这是一个编译时写法的问题,将上方的RequestMapping去掉,然后把路径放在下面的PostMapping 便可以正 ...