4.volatile用来修饰变量,就会让变量之间具备可见性。javascript
本次PTA做业题集多线程java
BallRunnable类实现了Runnable接口,重写了Run方法,支持多线程指派任务,使用Thread.sleep进行休眠是为了让咱们看清楚小球移动的轨迹,不然速度太快看不清楚。sql
是。安全
每一个小球能够从不一样位置出发,也就是随机出发,那就使用Math.random().多线程
private double x = Math.random()*50; private double y = Math.random()*50; private double dx = Math.random()*5; private double dy = Math.random()*5;
并回答:a)经过定义Runnable接口的实现类来实现多线程程序比经过继承自Thread类实现多线程程序有何好处?b) 6-1,6-3,6-11实验总结。dom
b).6-1 MyThread继承Thread,要重写run()。6-3.用Thread.currentThread().getName()能够获取当前的线程名字。6-11.这一题和6-1 Thread差很少,只不过一个是继承类,一个是实现接口。布局
Thread t1 = new Thread(() -> { System.out.println(mainThreadName); System.out.println(Thread.currentThread().getName()); System.out.println(Arrays.toString(Thread.class.getInterfaces())); });
要对word判空,刚开始作的·时候没有判空会抛出空指针的异常。
在while循环时加个flag控制跳出循环,不要用stop不安全。性能
使用Executors类的newFixedThreadPool()方法建立一个线程池。学习
用synchronizedList修饰list。this
完成题集6-4(互斥访问)与6-5(同步访问)
使用lock,unlock。
\\陈锦霞201621123061 public void deposit(int money) { lock.lock(); try { this.balance += money; condition.signal(); } finally { lock.unlock(); } } public void withdraw(int money) { lock.lock(); try { while (this.balance - money < 0) { try { wait(); } catch (InterruptedException e) { } } this.balance -= money; condition.signal(); } finally { lock.unlock(); } }
使用synchronized关键字修饰的方法叫同步方法,此时内置锁会保护整个方法。锁的范围比较大。
使用synchronized关键字修饰的语句块叫同步代码块。被该关键字修饰的语句块会自动被加上内置锁,锁的范围比较小,性能较好。
经过得到惟一的锁来打开资源,其余线程就先等待,notify()以后进入Lock Pool,使用资源以后,就把锁还回去。
th1得到id锁,读取值,th2等待,th1的id+1,将值传给id,锁被释放。th2得到锁,重复th1的步骤。
线程变化:多个线程进行争夺对象锁,一个线程得到对象锁,其他等待该线程结束。
wait()和notify()。
不正常。每次结果都是不肯定的。Comsumor和Producer的速度不同,同时进行存取操做。冲突了。
//陈锦霞201621123061 class Repository {// 存放字符串的仓库 private int capacity = 10;//仓库容量默认为10 private List<String> repo = new ArrayList<String>();// repo(仓库),最多只能放10个 public synchronized void add(String t) { while(repo.size() >= capacity) { System.out.println("仓库已满!没法添加货物。"); try{ wait(); }catch(Exception e){ e.printStackTrace(); } } repo.add(t); System.out.println("已添加!"); notify(); } public synchronized void remove() { while(repo.size() <= 0) { System.out.println("仓库无货!没法从仓库取货"); try{ wait(); }catch(Exception e){ e.printStackTrace(); } } repo.remove(0); System.out.println("已取走!"); notify(); }
//陈锦霞201621123061 class Repository {// 存放字符串的仓库 private int capacity = 10;//仓库容量默认为10 private List<String> repo = new ArrayList<String>();// repo(仓库),最多只能放10个 public synchronized void add(String t) { lock.lock(); while(repo.size() >= capacity) { System.out.println("仓库已满!没法添加货物。"); try{ condition.await(); }catch(Exception e){ e.printStackTrace(); } } repo.add(t); System.out.println("已添加!"); condition.signal(); lock.unlock(); } public synchronized void remove() { lock.lock(); while(repo.size() <= 0) { System.out.println("仓库无货!没法从仓库取货"); try{ condition.await(); }catch(Exception e){ e.printStackTrace(); } } repo.remove(0); System.out.println("已取走!"); condition.signal(); lock.unlock(); }
我本身
没有作出来,主要是时间过短,本身基础也并很差,模仿资料写的。
只有一小部分代码
import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class Login extends JFrame{ //声明标签、按钮、文本框和密码框 private JLabel JLb1; private JLabel JLb2; private JButton Ok_btn; private JButton Cancel_btn; private JTextField jtflduser; private JPasswordField jtpwdfld; //声明窗口 private JFrame frame; //构造方法 public Login(){ frame=new JFrame("登陆"); Container content=frame.getContentPane(); //采用GridLayout布局管理器 content.setLayout(new GridLayout(3,2,20,20)); JLb1=new JLabel("用户名"); JLb2=new JLabel("密 码"); //将标签置于居中位置 JLb1.setHorizontalAlignment(SwingConstants.CENTER); JLb2.setHorizontalAlignment(SwingConstants.CENTER); jtflduser=new JTextField(); jtpwdfld=new JPasswordField(); Ok_btn=new JButton("肯定"); Cancel_btn=new JButton("取消"); //为按钮增长事件监听者 Ok_btn.addActionListener(new ActionHandler()); Cancel_btn.addActionListener(new ActionHandler()); //添加标签、文本框和密码框到窗口 content.add(JLb1); content.add(jtflduser); content.add(JLb2); content.add(jtpwdfld); content.add(Ok_btn); content.add(Cancel_btn); frame.pack(); //设定登陆窗口启动时出如今屏幕中央位置 frame.setLocationRelativeTo(null); frame.setSize(300,200); frame.setVisible(true); }//Login() method /** *实现ActionListener监听,激活组件响应 */ class ActionHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ String str1,str2,sqlStr; Object obj=e.getSource(); //得到文本框和密码框的数据 str1=jtflduser.getText().trim(); str2=new String(jtpwdfld.getPassword()).trim(); try{ //点击肯定按钮 if(obj.equals(Ok_btn)){ if(str1.equals("")){ JOptionPane.showMessageDialog(frame,"用户名不能为空!"); return; } if(result.next()){ //弹出对话框提示登陆成功 JOptionPane.showMessageDialog(frame,"登陆成功!"); //打开图书管理主页面 bookmain bookmain1=new bookmain(); bookmain1.go(); //关闭登陆窗口 frame.dispose(); }else{ JOptionPane.showMessageDialog(frame,"用户名或密码错误"); } }else if(obj.equals(Cancel_btn)){ //点击取消按钮 System.exit(0); } }catch(ClassNotFoundException ce){ //捕获异常 System.out.println("SQLException:"+ce.getMessage()); } catch(SQLException ex){ //捕获异常 System.out.println(ex); } catch (Exception s) { //捕获异常 s.printStackTrace(); } } } public static void main(String args[]){ Login login=new Login(); }//main }//class ActionHandler
这是用户界面
3.码云及PTA
题目集:多线程
3.1. 码云代码提交记录
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 而后搜索并截图
必须出现几个要素:提交日期-用户名(姓名与学号)-不提交说明
3.2 截图"多线程"PTA提交列表
须要有两张图(1. 排名图。2.PTA提交列表图)
3.3 统计本周完成的代码量
须要将每周的代码统计状况融合到一张表中。
周次 | 行数 | 新增行数 | 文件数 | 新增文件数 |
---|---|---|---|---|
1 | 91 | 91 | 5 | 5 |
2 | 504 | 413 | 18 | 13 |
3 | 1092 | 588 | 28 | 10 |
5 | 1158 | 129 | 34 | 6 |
6 | 1539 | 381 | 40 | 6 |
7 | 2023 | 484 | 49 | 9 |
8 | 2477 | 454 | 57 | 8 |
9 | 2709 | 232 | 63 | 6 |
10 | 3156 | 447 | 70 | 7 |
11 | 3531 | 375 | 79 | 9 |
12 | 4083 | 552 | 91 | 12 |