上一页 1 2 3 4 5 6 7 ··· 21 下一页

java例程练习(多线程综合练习[生产者-消费者问题])

摘要: /* * 生产者与消费者问题 * 此问题用到Object类的wait(),notify() * 还要注意一个问题: * sleep()与wait()的区别: * sleep()过程中,对象仍未解锁 * wait ()过程中,对象解锁,其他线程可以访问当前对象,当唤醒时需重新锁定 */ public class Test { public static void main(String[] args) { ThingsStack ts = new ThingsStack(); Producer p = new Producer(ts); Consumer c ... 阅读全文
posted @ 2012-05-05 22:17 spring学习笔记 阅读(218) 评论(0) 推荐(0) 编辑

java例程练习(关于线程同步的补充)

摘要: /* * 从运行结果看,当m1()方法被锁定后,m2()方法仍然可以执行。 * 而且b的值被改变。由此可以得出结论: * sychronized 只是防止其定义的代码段被同时调用。 * */ public class Test implements Runnable{ int b = 100; public synchronized void m1() throws Exception { b = 1000; Thread.sleep(5000); System.out.println("b = " + b); } public void m2() ... 阅读全文
posted @ 2012-05-05 20:53 spring学习笔记 阅读(148) 评论(0) 推荐(0) 编辑

java例程练习(多线程[死锁问题])

摘要: /* *死锁产生有2个原因: *1,资源竞争 *2,进程间的推进顺序非法 * *程序模拟的是第一个情况 */ public class TestDeadLock implements Runnable { public int flag = 1; static Object o1 = new Object(); static Object o2 = new Object(); public void run() { System.out.println("flag=" + flag); if(flag == 1) { synchronized(o1) { ... 阅读全文
posted @ 2012-05-05 16:00 spring学习笔记 阅读(201) 评论(0) 推荐(0) 编辑

java例程练习(多线程[线程同步问题])

摘要: //线程同步问题 public class TestThread implements Runnable{ Timer timer = new Timer(); public static void main(String[] args) { TestThread test = new TestThread(); Thread t1 = new Thread(test); Thread t2 = new Thread(test); t1.setName("t1"); t2.setName("t2"); t1.start(); t2.start(); } 阅读全文
posted @ 2012-05-05 15:26 spring学习笔记 阅读(467) 评论(0) 推荐(0) 编辑

java例程练习(多线程[线程的优先级等等])

摘要: //设置线程的优先级 public class TestThread1 { public static void main(String[] args) { Thread t1 = new Thread(new T1()); Thread t2 = new Thread(new T2()); t1.setPriority(Thread.NORM_PRIORITY + 3); t1.start(); t2.start(); } } class T1 implements Runnable { public void run() { for(int i = 0;... 阅读全文
posted @ 2012-05-05 14:34 spring学习笔记 阅读(298) 评论(0) 推荐(0) 编辑

java例程练习(多线程[yield()方法])

摘要: public class Test { public static void main(String[] args) { MyThread m1 = new MyThread("m1"); MyThread m2 = new MyThread("m2"); m1.start(); m2.start(); } } class MyThread extends Thread { MyThread(String s) { super(s); } public void run() { for(int i = 1; i <= 100; i++) { Sys 阅读全文
posted @ 2012-05-04 23:26 spring学习笔记 阅读(162) 评论(0) 推荐(0) 编辑

java例程练习(多线程[join()方法])

摘要: public class Test { public static void main(String[] args) { MyThread myThread = new MyThread("m1"); myThread.start(); //产生分支,子线程开始执行 try{ myThread.join();//------等待合并myThread子线程,主线程才开始执行 } catch(InterruptedException e) {} for(int i = 1; i <= 10; i++) { System.out.println("I... 阅读全文
posted @ 2012-05-04 23:15 spring学习笔记 阅读(152) 评论(0) 推荐(0) 编辑

java例程练习(多线程[sleep()方法])

摘要: import java.util.*; public class Test { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); try { Thread.sleep(10000);//主线程睡眠 } catch(InterruptedException e) { } thread.interrupt(); //-----------不是最好的方法 //thread.flag = false; ... 阅读全文
posted @ 2012-05-04 23:01 spring学习笔记 阅读(235) 评论(0) 推荐(0) 编辑

java例程练习(多线程的两种创建方式)

摘要: //接口------推荐 public class Test { public static void main(String[] args) { Runner1 r = new Runner1(); //r.run();------->不是多线程,只是方法调用 Thread t = new Thread(r); t.start();//必须调用线程类的start()方法 //也可以这样: //new Thread(new Runner1()).start(); for(int i = 0; i < 100; i++) { ... 阅读全文
posted @ 2012-05-04 22:26 spring学习笔记 阅读(171) 评论(0) 推荐(0) 编辑

java例程练习(对象流)

摘要: import java.io.*; // transient 关键字 // serializable 接口 // externalizable 接口 public class Test { public static void main(String[] args) throws Exception{ T t = new T(); t.k = 8; FileOutputStream fos = new FileOutputStream("C:/java/testobjectio.txt"); ObjectOutputStream oos = new Obj... 阅读全文
posted @ 2012-05-04 17:31 spring学习笔记 阅读(154) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 ··· 21 下一页