多线程运行若干时间,主线程中断
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(); } } } }
相关文章推荐
- GDB 多线程调试:只停止断点的线程,其他线程任然执行; 或只运行某些线程 其他线程中断
- Java多线程下统计多个线程的运行时间
- java线程一之创建线程、线程池以及多线程运行时间统计
- 多核优化,使用linux affinity 将进程,线程,中断指定到对应的cpu运行,用ftrace查看消耗时间
- Java多线程下统计多个线程的运行时间
- Java怎样中断一个运行中的线程(1)
- Java怎样中断一个运行中的线程(1)
- 第二篇 多线程的使用——中断线程详解(Interrupt)
- 多线程--线程中断
- 《Java面向对象编程》学习笔记10——多线程中线程放弃运行态的几种方法
- 程序运行时候.中断.异步.还有多线程的一点小知识
- 【转载】Java怎样中断一个运行中的线程
- 实例说明:多进程和多线程 运行时间比较
- Java怎样中断一个运行中的线程
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到
- Java怎样中断一个运行中的线程(2)
- C++类的静态成员函数在多线程的工作机制以及运行过程中强制结束线程实验
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到
- Java怎样中断一个运行中的线程(2)
- Java怎样中断一个运行中的线程(3)