首页 技术 正文
技术 2022年11月11日
0 收藏 716 点赞 4,826 浏览 2708 个字

1. 本章学习总结

2. 书面作业

Q1.互斥访问与同步访问

完成题集4-4(互斥访问)与4-5(同步访问)

1.1除了使用synchronized修饰方法实现互斥同步访问,还有什么办法实现互斥同步访问(请出现相关代码)?

class Account{
private int balance;
private Lock poolLock = new ReentrantLock();
private Condition condition = poolLock.newCondition();
public Account(int balance) {
super();
this.balance = balance;
}
public int getBalance() {
return balance;
}
public void deposit(int money){
poolLock.lock();
try{
this.balance=getBalance() + money;
condition.signal();
}
finally
{
poolLock.unlock();
}
}
public void withdraw(int money){
poolLock.lock();
try{
while (getBalance() <money) {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.balance=getBalance() - money;
condition.signal();
}
finally{
poolLock.unlock();
}
}
}

1.2同步代码块与同步方法有何区别?

范围不同,前者范围小,后者范围大。

1.3 实现互斥访问的原理是什么?请使用对象锁概念并结合相应的代码块进行说明。当程序执行synchronized同步代码块或者同步方法时,线程的状态是怎么变化的?

当资源被一个任务访问时,锁定其资源,阻止其他线程访问,停止访问后,解锁。
class Counter {
private static int id = 0;public static void addId() {
synchronized (Counter.class) {
id++;
}}public static synchronized void subtractId() {
id--;
}public static int getId() {
return id;
}
}

1.4Java多线程中使用什么关键字实现线程之间的通信,进而实现线程的协同工作?为什么同步访问一般都要放到synchronized方法或者代码块中?

1.wait()和notify()来实现线程之间的通信,进而实现线程的协同工作。
2.放到synchronized方法或者代码块中可以避免了因同步访问而造成的共享资源不完整。

Q2.交替执行

实验总结(不管有没有做出来)

若是两个线程交替运行,需要使用wait()和notify()函数,还要在方法或代码块中加入synchronized关键字。
当任务1执行完后,改变flag为true,开始换任务2执行,任务2执行完后,改变flag为false,再换任务1执行,就这样实现交替执行。

Q3.互斥访问

3.1修改TestUnSynchronizedThread.java源代码使其可以同步访问。(关键代码截图,需出现学号)

class Counter {
private static int id = 0;public synchronized static void addId() {
id++;
}public synchronized static void subtractId() {
id--;
}public static int getId() {
return id;
}
}
//201521123019

3.2进一步使用执行器改进相应代码(关键代码截图,需出现学号)

public class TestUnSynchronizedThread {
public static void main(String[] args) throws InterruptedException {
ArrayList<Callable<Object>> tasks=new ArrayList<>();
ExecutorService executor =(ExecutorService)Executors.newCachedThreadPool();
for(int i=0;i<3;i++){
tasks.add(Executors.callable(new Adder()));
} for(int i=0;i<3;i++){
tasks.add(Executors.callable(new Subtracter()));
} executor.invokeAll(tasks);
System.out.println(Counter.getId());
System.out.println("main end");
}
}
//201521123019

Q4.线程间的合作:生产者消费者问题

4.1运行MyProducerConsumerTest.java。正常运行结果应该是仓库还剩0个货物。多运行几次,观察结果,并回答:结果正常吗?哪里不正常?为什么?

不正常,仓库内还有货物,因为消费者和生产者存取速度不同。

4.2使用synchronized, wait, notify解决该问题(关键代码截图,需出现学号)

 public synchronized void add(String t){
try{
while(repo.size() >= capacity){
wait();
System.out.println("仓库已满!无法添加货物。");
}
}catch(Exception e){
System.out.println(e);
}
repo.add(t);
notifyAll();
}public synchronized void remove(){
try{
while(repo.size() <= 0){
wait();
System.out.println("仓库无货!无法从仓库取货");
}
}catch(Exception e){
System.out.println(e);
}
repo.remove(0);
notifyAll();
}
//201521123019

Q5.查询资料回答:什么是线程安全?(用自己的话与代码总结,写自己看的懂的作业)

多线程访问同一段代码出来的结果与单线程访问代码的结果一致就是线程安全。

3.码云上代码提交记录

题目集:多线程(4-4到4-10)

3.1. 码云代码提交记录

3.2 截图多线程PTA提交列表

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298