摘要: package com.kk.thread;/* * 本类演示死锁的形成 * 基本数据类型是不能作为同步块的参考,例:int b;synchronized(b) */public class TicketsSystem { public static void main(String[] args)throws Exception { SellThread sell=new SellThread(); new Thread(sell).start(); Thread.sleep(1);//让thread1执行,此时b=false; ... 阅读全文
posted @ 2011-12-16 17:46 microsoft_kk 阅读(399) 评论(0) 推荐(0) 编辑
摘要: package com.kk.thread;/* * java使用抢占式调度模型 */public class ThreadTest { public static void main(String[] args) { MyThread myThread=new MyThread();//main线程的时间片完后,才执行Mythread的时间片 myThread.setDaemon(true); myThread.start(); myThread.setPriority(Thread.MAX_PRIORITY);//设置线程的执行... 阅读全文
posted @ 2011-12-16 17:12 microsoft_kk 阅读(446) 评论(0) 推荐(0) 编辑
摘要: package com.kk.thread;/** * 不调用stop方法退出线程 * */public class TestThread { public static void main(String[] args) { Thread1 thread=new Thread1(); thread.start(); int index=0; while(true){ if(index++==500){ thread.stopThread(); thr... 阅读全文
posted @ 2011-12-16 17:03 microsoft_kk 阅读(218) 评论(0) 推荐(0) 编辑
摘要: package com.kk.thread;public class Test { public static void main(String[] args) { Queue q=new Queue(); Producer p=new Producer(q); Consumer c=new Consumer(q); c.start(); p.start(); }}class Producer extends Thread { Queue q; public Producer(Queue q) { ... 阅读全文
posted @ 2011-12-16 17:00 microsoft_kk 阅读(279) 评论(0) 推荐(0) 编辑
摘要: package com.kk.thread;/* * 继承Thread和实现Runnable接口都可以实现多线程 * 继承Thread可以修改线程的一些属性,例如name、daemon等 * 一般采用实现Runnable接口 */public class RunableTest { public static void main(String[] args) { RunnableClassTest r = new RunnableClassTest();// new Thread(r).start();// new Thread(r).start... 阅读全文
posted @ 2011-12-16 16:32 microsoft_kk 阅读(631) 评论(0) 推荐(0) 编辑