《Java程序设计》第十一次实验总结


  1. 本周学习总结

1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容。

  1. 书面作业

  1. 源代码阅读:多线程程序BounceThread

1.1 BallRunnable类有什么用?为什么代码中需要调用Thread.sleep进行休眠?

BallRunnable类实现了Runnable接口,支持多线程。

调用Thread.sleep休眠是为了在这个线程停止的这段时间内运行别的线程。

1.2 Ball.java只做了两件事,这两件事分别是什么?BallComponent对象是干什么的?其内部的ArrayList有什么用?程序运行过程中,生成了几个BallComponent对象?该程序使用了多线程技术,每个小球是分别在不同的线程中进行绘制吗?

其中Ball.java定义了move()函数用于实现小球的移动方法,定义了getshape()函数来获取小球的大小和坐标。BallComponent对象是用来添加和画出一个球,内部的ArrayList来添加小球,程序运行过程中生成了一个BallComponent对象。每个小球是在不同的线程中绘制的,因为没start一次,就启动了一个新的线程。

  1. 实验总结:题集(多线程)

2.1 题目:Thread、PrintTask、Runnable与匿名内部类。

并回答:a)通过定义Runnable接口的实现类来实现多线程程序比通过继承自Thread类实现多线程程序有何好处?b) 6-1,6-3,6-11实验总结。

1):适合多个相同的程序代码的线程去处理同一个资源

2):可以避免java中的单继承的限制

3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立

4):线程池只能放入实现Runable或callable类线程,不能直接放入继承Thread的类

class Ticket implements Runnable

{

int tk=10;

public void run()

{

while(tk>0)

{

System.out.println(Thread.currentThread().getName()+"窗口正在售出"+tk--+"号票.");

}

}

}

public class TicketMan {
public static void main(String[]aresg)
{
Ticket t=new Ticket();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}

2.2 使用Lambda表达式改写6-3

Thread t1 = new Thread(() -> {
System.out.println(mainThreadName);
System.out.println(Thread.currentThread().getName());
System.out.println(Arrays.toString(Thread.class.getInterfaces()));
});

2.3 题目:6-2(Runnable与停止线程)。回答:需要怎样才能正确地停止一个运行中的线程?

当想停止一个运行中的线程时可以实用一个boolean类型的变量来终止,然后使用while()语句,在语句中改变boolean来使while循环退出来停止线程。

  1. 互斥访问

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

4. 互斥访问与同步访问

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

4.1 除了使用synchronized修饰方法实现互斥同步访问,还有什么办法可以使用synchronized实现互斥同步访问,使用代码说明(请出现相关代码及学号)?

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; class Account {
private int balance;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public Account(int balance) {
super();
this.balance = balance;
}
public int getBalance() {
return balance;
}
public void deposit(int money) {
lock.lock();
try {
balance += money;
condition.signalAll();
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
public void withdraw(int money) {
lock.lock();
try {
while (balance < money) {
try {
condition.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
balance -= money;
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
}

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

同步方法是在方法名前面加synchonrized关键字,为整个方法加上内置锁,从而实现同步,同步代码块是用synchonrized修饰的语句块,只是为所修饰的语句块加锁来实现同步,所以它们的范围不同

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

原理是当资源被一个任务使用时加锁

public class ThreadSafeTest implements Runnable {
int num = 10; public void run() {
while (true) {
synchronized ("") {
if (num > 0) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("tickets" + --num);
}
}
}
} public static void main(String[] args) {
ThreadSafeTest t = new ThreadSafeTest();
Thread tA = new Thread(t);
Thread tB = new Thread(t);
Thread tC = new Thread(t);
Thread tD = new Thread(t);
tA.start();
tB.start();
tC.start();
tD.start();
}
}

当运行程序时,一个线程先开始工作,给num上了一把锁,然后其他线程便不能访问这个区域,等执行完售票的操作将票数减一并输出后,对象锁被释放,然后下一个线程开始工作,再给num上了一把锁,然后直到票数为0结束进程

4.4 Java多线程中使用什么关键字实现线程之间的通信,进而实现线程的协同工作?

使用synchronized关键字;wait()方法、notify()方法;

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

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



结果不正常,如图所示,出现了货物还剩余的现象,这是因为Producer和customer的存取速度不同,最终导致货物还剩余。

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

6. 面向对象设计作业-图书馆管理系统

6.1 系统的功能模块表格,表格中体现出每个模块的负责人。

6.2 运行视频

6.3 讲解自己负责的模块,并粘贴自己负责模块的关键代码(出现学号及姓名)。

3.码云及PTA

3.1. 码云代码提交记录

3.2 截图"多线程"PTA提交列表

3.3 统计本周完成的代码量

周次 总代码量 新增代码量 总文件数 新增文件数
2 381 381 12 5
3 661 280 19 7
4 974 313 24 5
5 1358 384 33 9
6 2211 853 37 4
7 3223 412 42 5
8 3635 423 46 4
9 3867 232 51 5
10 3997 130 55 4

最新文章

  1. win7升win10,初体验
  2. [wxWidgets] 1. 安装及&quot;hello world&quot;程序
  3. adb opendir failed ,permission denied
  4. 一个可以设置所有子控件是否可以点击的Layout的实现
  5. javascript —— HTTP头文件详解
  6. React Native开发环境搭建
  7. Mysql之Windows初探
  8. Git学习 -- 冲突解决
  9. Unity运动残影技能
  10. FFPLAY的原理
  11. 关于 GET、POST、表单、Content-Type
  12. SE Springer小组之《Spring音乐播放器》可行性研究报告五、六
  13. Expm 9_1 有向图中环的判断问题
  14. 脚手架搭建vue框架
  15. python: &quot;TypeError: &#39;type&#39; object is not subscriptable&quot;
  16. 并发编程 —— ScheduledThreadPoolExecutor
  17. leetcode179
  18. flv网页视频播放
  19. 理解 python 装饰器
  20. jzoj100029

热门文章

  1. DIY自己的GIS程序(1)——起航
  2. 文本IO 二进制IO
  3. 接口测试之接口api文档的重要性
  4. mysql索引之聚簇索引与非聚簇索引
  5. 一次org.springframework.jdbc.BadSqlGrammarException ### Error querying database Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException问题排查过程
  6. Python numpy有什么用?
  7. mysql数据库设置不区分大小写,启动方法
  8. deepin中crossover或playonlinux装完office后word无法输入中文的问题
  9. Cooperation.GTST团队第四周项目总结
  10. Ubuntu18.04 怎么开热点