摘要: public class ProducerConsumer { public static void main(String[] args) { SyncStack ss = new SyncStack(); Producer p = new Producer(ss); Consumer c = new Consumer(ss); new Thread(p).start(); new Thread(p).start(); new Thread(p).start(); new Thread(c... 阅读全文
posted @ 2012-03-19 23:19 haiwei.sun 阅读(114) 评论(0) 推荐(0) 编辑
摘要: package org.shw.pc;public class TestPC { public static void main(String[] args) { Info info = new Info(); Producer pro = new Producer(info); Consumer con=new Consumer(info); new Thread(pro).start(); new Thread(con).start(); }}生产者消费者--Producer.java 阅读全文
posted @ 2012-03-19 21:57 haiwei.sun 阅读(135) 评论(0) 推荐(0) 编辑
摘要: package org.shw.pc;public class Producer implements Runnable { private Info info=null; public Producer(Info info){ this.info = info; } public void run() { for(int x=0;x<100;x++){ if(x%2==0){ this.info.set("MLDN","www.mldnjava.cn"); }... 阅读全文
posted @ 2012-03-19 21:56 haiwei.sun 阅读(148) 评论(0) 推荐(0) 编辑
摘要: package org.shw.pc;public class Consumer implements Runnable { private Info info=null; public Consumer(Info info){ this.info=info; } public void run() { for(int x=0;x<100;x++){ this.info.get(); } }} 阅读全文
posted @ 2012-03-19 21:56 haiwei.sun 阅读(156) 评论(0) 推荐(0) 编辑
摘要: package org.shw.pc;public class Info { private String title="李兴华"; private String content="java讲师"; private boolean flag=false; //false--表示可以取走,但是不能生产 true--表示可以生产,但是不能取走 public synchronized void set(String title,String content){ if(flag==false){//已经生产过了,需要等待 try{ ... 阅读全文
posted @ 2012-03-19 21:55 haiwei.sun 阅读(227) 评论(0) 推荐(0) 编辑
摘要: 新建状态:用new语句创建的线程对象处于新建状态。就绪状态:当一个线程对象创建后,其他线程调用它的start()方法,该线程就进入就绪状态。运行状态:处于这个状态的线程占用CPU,执行程序代码。只有处于就绪状态的线程才有机会转到运行状态。阻塞状态:线程因为某些原因放弃CPU,暂时停止运行。当线程处于阻塞状态时,Java虚拟机不会给线程分配CPU,直到线程重新进入就绪状态,它才有机会转到运行状态。死亡状态:当线程执行完run()方法中的代码,或者遇到了未捕获的异常,就会退出run()方法,此时就进入死亡状态,该线程结束生命周期。程序如何干预Java虚拟机对线程的调度过程(1)调整各个线程的优先级 阅读全文
posted @ 2012-03-19 15:54 haiwei.sun 阅读(214) 评论(0) 推荐(0) 编辑
摘要: public class ThreadJoin extends Thread { public void run(){ for(int a=0;a<30;a++){ System.out.println(getName()+":"+a); } } public static void main(String[] args) { ThreadJoin tj = new ThreadJoin(); tj.setName("m1"); tj.start(); Syste... 阅读全文
posted @ 2012-03-19 14:04 haiwei.sun 阅读(326) 评论(0) 推荐(0) 编辑
摘要: public class ThreadYield extends Thread{ public void run(){ for(int a=0;a<50;a++){ yield(); System.out.println(Thread.currentThread().getName()+" : "+a); } } public static void main(String[] args) { ThreadYield t1 = new ThreadYield(); Th... 阅读全文
posted @ 2012-03-19 13:47 haiwei.sun 阅读(668) 评论(0) 推荐(0) 编辑
摘要: 两者的区别有: 1、最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法。 2、这两个方法来自不同的类分别是Thread和Object 3、wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在 任何地方使用 synchronized(x){ x.notify() //或者wait() } 4、sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常 阅读全文
posted @ 2012-03-19 13:21 haiwei.sun 阅读(1029) 评论(0) 推荐(0) 编辑
摘要: public class ThreadSleep extends Thread { public void run(){ for(int a=0;a<50;a++){ try{Thread.sleep(500);}catch(InterruptedException e){} System.out.println(Thread.currentThread().getName()+" : "+a); } } public static void main(String[] args) { ... 阅读全文
posted @ 2012-03-19 12:35 haiwei.sun 阅读(17611) 评论(0) 推荐(0) 编辑
摘要: public class MyRunnable implements Runnable{ int a=0; public void run(){ for(a=0;a<10;a++){ try{Thread.sleep(100);}catch(Exception e){} System.out.println(Thread.currentThread().getName()+":"+a); } } public static void main(String[] args) { MyRu... 阅读全文
posted @ 2012-03-19 12:26 haiwei.sun 阅读(144) 评论(0) 推荐(0) 编辑
摘要: public class MyThread extends Thread { public void run(){ for(int i=0;i<50;i++){ try{ Thread.sleep(100);}catch(Exception e){} System.out.println(Thread.currentThread().getName()+" : "+i); } } public static void main(String[] args) { MyThread t1 =... 阅读全文
posted @ 2012-03-19 11:34 haiwei.sun 阅读(245) 评论(0) 推荐(0) 编辑
返回顶部