练习16和练习15的要求一样。只不过同步的方式要用Lock。代码如下:
package exercise.exercise16;
public interface ThrillingInter {
void aInc();
void bInc();
void cInc();
}
package exercise.exercise16;
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.exercise16;
import java.util.concurrent.locks.ReentrantLock;
public class Thrilling_SynchronizedOneObject implements ThrillingInter {
int count;
ReentrantLock lock = new ReentrantLock();
final Object object = new Object();
public void aInc() {
lock.lock();
try {
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!");
} finally {
lock.unlock();
}
}
public void bInc() {
lock.lock();
try {
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!");
} finally {
lock.unlock();
}
}
public void cInc() {
lock.lock();
try {
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!");
} finally {
lock.unlock();
}
}
}
package exercise.exercise16;
import java.util.concurrent.locks.ReentrantLock;
public class Thrilling_SynchronizedThredOjbect implements ThrillingInter {
int count;
final ReentrantLock object = new ReentrantLock();
final ReentrantLock object1 = new ReentrantLock();
final ReentrantLock object2 = new ReentrantLock();
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-0 into cInc()! count is :0
cInc completed!
Thread-1 into aInc()! count is 1
aInc completed!
Thread-2 into bInc()! count is :2
bInc completed!
Thread-0 into cInc()! count is :0
Thread-1 into aInc()! count is 1
Thread-2 into bInc()! count is :2
bInc completed!
cInc completed!
aInc completed!