问题:利用Java多线程,轮流打印数字,也就是怎么控制线程....

1:通过synchronized的关键字,对类的static final 成员进行Lock,锁住对象,来实现同步。

    private int id;
private static int n = 0;
private static final ThreadTest lock = new ThreadTest(); public ThreadTest(int id) {
this.id = id;
}
public ThreadTest () { }
@Override
public void run() {
while (n < 100) {
synchronized (lock) {
while ((n % 5 == 0) && (n / 5) % 5 != id) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (n < 100) {
System.out.print("Thread-" + (id+1) + " : " + " " + (n + 1)
+ " " + (n + 2) + " " + (n + 3) + " " + (n + 4)
+ " " + (n + 5) + "\n");
n += 5;
}
lock.notifyAll();
}
}
}

2.通过AtomicInteger对象,和ExecutorService实现线程之间的同步

    private AtomicInteger atomicInteger=new AtomicInteger(0);
private static final int max=20; class Thread1 implements Runnable{
private int mark=0;
public Thread1(int i){
this.mark=i;
}
public void run() {
while(atomicInteger.get()<max){
if(atomicInteger.get()%5==mark){
System.out.println("线程Thread"+(mark+1)+"打印:"+(atomicInteger.get()*5+1)+" "
+(atomicInteger.get()*5+2)+" "+(atomicInteger.get()*5+3)+" "
+(atomicInteger.get()*5+4)+" "+(atomicInteger.get()*5+5));
atomicInteger.getAndIncrement();
}
}
}
}

3.通过ReentrantLock对象和Condition对象实现线程之间的同步

private  int state = 1;
private int n = 1;
private ReentrantLock lock=new ReentrantLock();
private Condition condition1=lock.newCondition();
private Condition condition2=lock.newCondition();
private Condition condition3=lock.newCondition();
@Override
public void run(){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
try {
lock.lock();
while(state!=1)
try{
condition1.await();
}
catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.print(Thread.currentThread().getName()+": ");
for (int j = 0; j < 5; j++) {
System.out.print(n+++" ");
}
System.out.println();
state=2;
condition2.signal();
} finally{
lock.unlock();
}
}
}
},"线程1").start(); new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
try{
lock.lock();
while(state!=2)
try {
condition2.await();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.print(Thread.currentThread().getName()+": ");
for (int j = 0; j < 5; j++) {
System.out.print(n+++" ");
}
System.out.println();
state=3;
condition3.signal();
}
finally{
lock.unlock();
}
}
}
},"线程2").start(); new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
try{
lock.lock();
while(state!=3)
try {
condition3.await();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.print(Thread.currentThread().getName()+": ");
for (int j = 0; j < 5; j++) {
System.out.print(n+++" ");
}
System.out.println();
state=1;
condition1.signal();
}finally{
lock.unlock();
}
}
}
},"线程3").start();
}

最新文章

  1. javascript面试题:如何把一句英文每个单词首字母大写?
  2. HTML—marquee
  3. 场景9 深入RAC运行原理
  4. VS2008基于对话框的MFC上位机串口通信(C++实现)简单例程
  5. 鼠标悬浮图片时弹出透明提示图层的jQuery特效
  6. ADO.NET通用数据库访问类
  7. MySQL源码:索引相关的数据结构
  8. oracle的一知半解
  9. python笔记之subprocess模块
  10. tera term 残ALT债券
  11. java设计模式之一工厂模式
  12. Mac, Linux中配置Latex中文字体
  13. Mysql数据库主从配置
  14. hdu 5052 树链剖分
  15. Dynamics CRM2011 通过DeveloperToolkit在VS中部署遇到的问题
  16. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-3项目架构说明
  17. Java 输入/输出——处理流(ObjectIO)
  18. WinForm下窗体权限设计
  19. Redux DevTools浏览器插件调试redux
  20. Python 工匠:编写条件分支代码的技巧

热门文章

  1. Git打包文件
  2. DECO 一个REACT NAtive 开发IDE工具
  3. android studio从1.5更新到2.0后terminal无法运行gradle命令,提示无法找到gradle命令
  4. hdu 1002.A + B Problem II 解题报告
  5. 【leetcode】Binary Tree Level Order Traversal I &amp; II
  6. 【leetcode】Kth Largest Element in an Array (middle)☆
  7. August 27th 2016 Week 35th Saturday
  8. 面向对象的小demo
  9. 关于KVC、KVO
  10. MyString(重写String)