您的位置:首页 > 编程语言 > Java开发

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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: