JAVA编程思想第四版-多线程的练习答案之练习15

来源:互联网 发布:天下三女角色捏脸数据 编辑:程序博客网 时间:2024/06/05 02:11
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  :0bInc completed!Thread-2  into bInc()! count is  :1bInc completed!Thread-0  into bInc()! count is  :2bInc completed!
可以同时运行多个方法:

Thread-0  into cInc()! count is :0Thread-1  into bInc()! count is  :1cInc completed!bInc completed!Thread-2  into bInc()! count is  :2bInc completed!




原创粉丝点击