一个多线程例子
2013-01-15 17:17
211 查看
class Info { public synchronized void hold() throws InterruptedException { this.wait(); } public synchronized void run() { this.notifyAll(); } } public class TestThread extends Thread { public int i = 0; public Info info; public int stop = 0; public int start = 0; public int getStop() { return stop; } public void setStop(int stop) { this.stop = stop; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public TestThread(Info info, int stop, int start) { this.stop = stop; this.start = start; this.info = info; } @Override public void run() { while (i < 20) { if (i == stop) { try { info.hold(); } catch (InterruptedException e) { e.printStackTrace(); } } if(i==start){ info.run(); } i++; System.out.println(Thread.currentThread().getName()+":"+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Info info = new Info(); TestThread t1 = new TestThread(info, 8, -1); TestThread t2 = new TestThread(info,5,-1); TestThread t3 = new TestThread(info,-1,10); t1.setName("a"); t2.setName("b"); t3.setName("main"); t3.start(); t1.start(); t1.setStop(5); t2.start(); } }
相关文章推荐
- php pthreads 多线程扩展的使用:一个较为稳定例子。
- 标准C / C + +运行期库原先并不是设计用于多线程应用程序的唯一一个例子
- 一个例子玩转linux多线程问题(创建 执行 条件变量 ......)
- java中给出一个多线程TCP的ServerSocket例子?
- 多线程的一个例子
- 一个多线程例子,使用CountDownLatch
- MFC 多线程的一个例子
- 一个多线程的经典例子
- 一个很好的Java多线程的例子
- 一个多线程的小例子---C#高级编程学习
- 分享一个多线程实现[冒泡][选择][二分法]排序的例子
- 一个例子展开,介绍Linux下面线程的操作、多线程的同步和互斥。
- Java 多线程编程之五:一个理解 wait() 与 notify() 的例子
- 一个关于多线程的简单例子(两种实现方法)
- 笔试题11:编写一个生产者与消费者的多线程例子(源码)
- qt多线程一个简单的例子
- 一个多线程绑定到不同cpu上的例子
- php pthreads 多线程扩展的使用:一个较为稳定例子。
- 利用Semaphore实现多线程的一个例子
- 关于多线程的一个例子(UI实时显示)