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 |