A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Conning 中级黑马   /  2014-5-12 20:46  /  1886 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我在做一个题目,第一个线程打印1 2 3 4 5 第二个线程打印 6 7 8 9 10 第三个线程打印 11 1213 14 15 如何交替下去打印到75啊

4 个回复

倒序浏览
代码太麻烦了.思想就是  你设置一个计数器,比如 当i=1 的时候 只有线程1能打印,打印完后 将i自增
然后当i=2的时候 只能线程2打印,打印完再自增.当i=3的时候线程3打印,打印完后将i重置为1就行了
回复 使用道具 举报
做出来了,没有用到Lock接口。。。只用同步代码块实现了

class ThreadPrint implements Runnable
{
int count=1;
int k=1;
private void sop()
{
for(int i=0;i<5;i++)
{
System.out.println(Thread.currentThread().getName()+":"+count++);
}
}

public void run()
{
while(count<=75)
{
synchronized(this)
{
if(k==1)
{
if(Thread.currentThread().getName().equals("线程1"))
{

sop();
k=(k+1)%3;
this.notifyAll();
try{this.wait();}catch(Exception e){}

}
else
{
try{this.wait();}catch(Exception e){};
}
}
else if(k==2)
{
if(Thread.currentThread().getName().equals("线程2"))
{

sop();
k=(k+1)%3;
this.notifyAll();
try{this.wait();}catch(Exception e){}

}
else
{
try{this.wait();}catch(Exception e){};
}
}
else
{
if(Thread.currentThread().getName().equals("线程3"))
{

sop();
k=(k+1)%3;
if(count>75)
{
this.notifyAll();
}
else
{
this.notifyAll();
try{this.wait();}catch(Exception e){}
}

}
else
{
try{this.wait();}catch(Exception e){};
}
}
}
}

}
}


public class ThreadTest
{
public static void main(String args [])throws Exception
{
ThreadPrint tp=new ThreadPrint();

Thread t1=new Thread(tp,"线程1");
Thread t2=new Thread(tp,"线程2");
Thread t3=new Thread(tp,"线程3");

t1.start();
t2.start();
t3.start();

}
}
回复 使用道具 举报
这个随便弄的好像也行。。。
  1. package cn.itcast.day;


  2. public class ThreadTest {

  3.         public static void main(String[] args) {
  4.                 // TODO Auto-generated method stub
  5.                 My_Print mp = new My_Print();
  6.                 new Thread(mp,"1").start();
  7.                 new Thread(mp,"2").start();
  8.                 new Thread(mp,"3").start();
  9.         }
  10. }
  11. class My_Print implements Runnable{
  12.         private int num = 1;
  13.         private int k = 1;
  14.         public void print(){
  15.                 for(int x = 0; x < 5 ;x++)
  16.                         System.out.println(Thread.currentThread().getName()+ "::" + num++);
  17.         }
  18.         @Override
  19.         public void run() {
  20.                 // TODO Auto-generated method stub
  21.                 while(num <= 75){
  22.                         if(k == 1&&Thread.currentThread().getName().equals("1")&& num <= 75){
  23.                                 print();
  24.                                 k = 2;
  25.                         }
  26.                         if(k == 2&&Thread.currentThread().getName().equals("2")&& num <= 75){
  27.                                 print();
  28.                                 k = 3;       
  29.                         }         
  30.                         if(k == 3&&Thread.currentThread().getName().equals("3")&& num <= 75){
  31.                                 print();                                       
  32.                                 k = 1;
  33.                         }
  34.                 }       
  35.         }
  36.        
  37. }
复制代码
回复 使用道具 举报
你创建类 实现Runnable 这个接口就可以了, 你好好看看视频啊。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马