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

本文介绍了一种使用Lock来实现线程同步的方法,并通过具体示例代码展示了如何利用ReentrantLock来确保线程安全地访问共享资源。两种不同的同步方式被实现并比较:一种允许同一时间仅执行一个方法,另一种则允许多个方法同时运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习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!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值