JAVA编程思想第四版-多线程的练习答案之练习15
2013-10-31 21:49
447 查看
package exercise.exercise15; public interface ThrillingInter { void aInc(); void bInc(); void cInc(); }
package exercise.exercise15; public class MainThread extends Thread { static int point; //private static final ThrillingInter thrilling = new Thrilling_SynchronizedOneObject();//只能同时运行一个方法 private static final ThrillingInter thrilling = new Thrilling_SynchronizedThredOjbect();//同时运行多个方法 public static void main(String[] args) { for (point = 2; point <= 4; point++) { new MainThread().start();//同步一个对象。只能同时运行一个方法! } } public void run() { if (point % 3 == 1) { // 如果point整除剩余1,则此线程执行a方法 thrilling.aInc(); } else if (point % 3 == 2) { // 如果point整除剩余2,则此线程执行b方法 thrilling.bInc(); } else if (point % 3 == 0) { // 如果point整除,则此线程执行c方法 thrilling.cInc(); } } }
package exercise.exercise15; public class Thrilling_SynchronizedOneObject implements ThrillingInter { int count; final Object object = new Object(); public void aInc() { synchronized (object) { System.out.println(Thread.currentThread().getName() + " into aInc()! count is " + count++); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("aInc completed!"); } } public void bInc() { synchronized (object) { System.out.println(Thread.currentThread().getName() + " into bInc()! count is :" + count++); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("bInc completed!"); } } public void cInc() { synchronized (object) { System.out.println(Thread.currentThread().getName() + " into cInc()! count is :" + count++); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("cInc completed!"); } } }
package exercise.exercise15; public class Thrilling_SynchronizedThredOjbect implements ThrillingInter { int count; final Object object = new Object(); final Object object1 = new Object(); final Object object2 = new Object(); public void aInc() { synchronized (object) { System.out.println(Thread.currentThread().getName() + " into aInc()! count is " + count++); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("aInc completed!"); } } public void bInc() { synchronized (object1) { System.out.println(Thread.currentThread().getName() + " into bInc()! count is :" + count++); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("bInc completed!"); } } public void cInc() { synchronized (object2) { System.out.println(Thread.currentThread().getName() + " into cInc()! count is :" + count++); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("cInc completed!"); } } }
执行结果:
同时只能运行一个方法:
Thread-1 into bInc()! count is :0 bInc completed! Thread-2 into bInc()! count is :1 bInc completed! Thread-0 into bInc()! count is :2 bInc completed!
可以同时运行多个方法:
Thread-0 into cInc()! count is :0 Thread-1 into bInc()! count is :1 cInc completed! bInc completed! Thread-2 into bInc()! count is :2 bInc completed!
相关文章推荐
- JAVA编程思想第四版-多线程的练习答案之练习2
- JAVA编程思想第四版-多线程的练习答案之练习3
- JAVA编程思想第四版-多线程的练习答案之练习4
- JAVA编程思想第四版-多线程的练习答案之练习5
- JAVA编程思想第四版-多线程的练习答案之练习6
- JAVA编程思想第四版-多线程的练习答案之练习18
- JAVA编程思想第四版-多线程的练习答案之练习7
- JAVA编程思想第四版-多线程的练习答案之练习12
- JAVA编程思想第四版-多线程的练习答案之练习19
- JAVA编程思想第四版-多线程的练习答案之练习8
- JAVA编程思想第四版-多线程的练习答案之练习13
- JAVA编程思想第四版-多线程的练习答案之练习20
- JAVA编程思想第四版-多线程的练习答案之练习9
- JAVA编程思想第四版-多线程的练习答案之练习16
- JAVA编程思想第四版-多线程的练习答案之练习17
- JAVA编程思想第四版-多线程的练习答案之练习1
- JAVA编程思想第四版-多线程的练习答案之练习11
- Java编程思想第四版第五章练习
- Java-Java编程思想第四版 第十二章 练习
- Java-Java编程思想第四版 第十六章 数组 练习