您的位置:首页 > 其它

多线程运行若干时间,主线程中断

2014-06-16 17:28 267 查看
import java.util.ArrayList;
import java.util.List;

public class ThreadConnection
{
public static void main(String args[])
{
DataTest da=new DataTest();
Thread thr1=new Thread(da);
Thread thr2=new Thread(da);
Thread thr3=new Thread(da);
Thread thr4=new Thread(da);
thr1.start();
thr2.start();
thr3.start();
thr4.start();
try {
Thread.sleep((long)Math.random()*10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread.currentThread().interrupt();
}
}
class DataTest implements Runnable
{
int index=0;
@Override
public  void run()
{
ArrayList<String> list=new ArrayList<String>();
list.add("red");
list.add("blue");
list.add("yellow");
list.add("green");
list.add("color");
// TODO Auto-generated method stub
synchronized(this)
{
System.out.println(list.get(index++));
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();//这里有点问题,大家可以帮我修改一下
try
{
Thread.sleep((long)Math.random()*10000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐