馬昕璐 201771010118《面向物件程式設計(java)》第十六週學習總結
第一部分:理論知識學習部分
程式:一段靜態的程式碼,應用程式執行的藍本。
程序:是程式的一次動態執行,它對應了從程式碼載入、執行至執行完畢的一個完整過程。
多執行緒:程序執行過程中產生的多條執行線索,比程序執行更小的單位。
執行緒不能獨立存在,必須存在於程序中,同一程序的各執行緒間共享程序空間的資料。
每個執行緒有它自身的產生、存在和消亡的過程, 是一個動態的概念。
多執行緒意味著一個程式的多行語句可以看上去幾 乎在同一時間內同時執行。
執行緒建立、銷燬和切換的負荷遠小於程序,又稱 為輕量級程序
Java實現多執行緒有兩種途徑:
‐建立Thread類的子類
‐在程式中定義實現Runnable介面的類
用Thread類的子類建立多執行緒的關鍵性操作:
–定義Thread類的子類並實現使用者執行緒操作,即 run()方法的實現。
–在適當的時候啟動執行緒。
由於Java只支援單重繼承,用這種方法定義的類不 可再繼承其他父類。
二、實驗部分
1、實驗目的與要求
(1) 掌握執行緒概念;
(2) 掌握執行緒建立的兩種技術;
(3) 理解和掌握執行緒的優先順序屬性及排程方法;
(4) 掌握執行緒同步的概念及實現技術;
2、實驗內容和步驟
實驗1:測試程式並進行程式碼註釋。
測試程式1:
l 在elipse IDE中除錯執行
l 掌握執行緒概念;
l 掌握用Thread的擴充套件類實現執行緒的方法;
l 利用Runnable介面改造程式,掌握用Runnable介面建立執行緒的方法。
class Lefthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("You are Students!"); try{ sleep(500); } catch(InterruptedException e) { System.out.println("Lefthand error.");} } } } class Righthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("I am a Teacher!"); try{ sleep(300); } catch(InterruptedException e) { System.out.println("Righthand error.");} } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { left=new Lefthand(); right=new Righthand(); left.start(); right.start(); } } |
Runnable介面改造的程式:
class Lefthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("You are Students!"); try{ sleep(500); }//在指定的毫秒數內讓當前正在執行的執行緒休眠(暫停執行 catch(InterruptedException e) //當執行緒在活動之前或活動期間處於正在等待、休眠或佔用狀態且該執行緒被中斷時,丟擲該異常。有時候,一種方法可能希望測試當前執行緒是否已被中斷,如果已被中斷,則立即丟擲此異常。 { System.out.println("Lefthand error.");} } } } class Righthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("I am a Teacher!"); try{ sleep(300); } catch(InterruptedException e) { System.out.println("Righthand error.");} } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { Runnable left1 = new Lefthand(); Runnable right1 = new Righthand() ; Thread left = new Thread(left1); Thread right = new Thread(right1); left.start();//使該執行緒開始執行;Java 虛擬機器呼叫該執行緒的 run 方法。 right.start(); } }
執行結果均為:
測試程式2:
l 在Elipse環境下除錯教材625頁程式14-1、14-2 、14-3,結合程式執行結果理解程式;
l 在Elipse環境下除錯教材631頁程式14-4,結合程式執行結果理解程式;
l 對比兩個程式,理解執行緒的概念和用途;
l 掌握執行緒建立的兩種技術。
package bounce; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Shows an animated bouncing ball. * @version 1.34 2015-06-21 * @author Cay Horstmann */ public class Bounce { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new BounceFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } Bounce
package bounce; import java.awt.*; import java.util.*; import javax.swing.*; /** * The component that draws the balls. * @version 1.34 2012-01-26 * @author Cay Horstmann */ public class BallComponent extends JPanel { private static final int DEFAULT_WIDTH = 450; private static final int DEFAULT_HEIGHT = 350; private java.util.List<Ball> balls = new ArrayList<>(); /** * Add a ball to the component. * @param b the ball to add */ public void add(Ball b) { balls.add(b); } public void paintComponent(Graphics g) { super.paintComponent(g); // erase background Graphics2D g2 = (Graphics2D) g; for (Ball b : balls) { g2.fill(b.getShape()); } } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
package bounce; import java.awt.geom.*; /** * A ball that moves and bounces off the edges of a rectangle * @version 1.33 2007-05-17 * @author Cay Horstmann */ public class Ball { private static final int XSIZE = 15; private static final int YSIZE = 15; private double x = 0; private double y = 0; private double dx = 1; private double dy = 1; /** * Moves the ball to the next position, reversing direction if it hits one of the edges */ public void move(Rectangle2D bounds) { x += dx; y += dy; if (x < bounds.getMinX()) { x = bounds.getMinX(); dx = -dx; } if (x + XSIZE >= bounds.getMaxX()) { x = bounds.getMaxX() - XSIZE; dx = -dx; } if (y < bounds.getMinY()) { y = bounds.getMinY(); dy = -dy; } if (y + YSIZE >= bounds.getMaxY()) { y = bounds.getMaxY() - YSIZE; dy = -dy; } } /** * Gets the shape of the ball at its current position. */ public Ellipse2D getShape() { return new Ellipse2D.Double(x, y, XSIZE, YSIZE); } }
執行結果:
測試程式3:
分析以下程式執行結果並理解程式。
class Race extends Thread { public static void main(String args[]) { Race[] runner=new Race[4]; for(int i=0;i<4;i++) runner[i]=new Race( ); for(int i=0;i<4;i++) runner[i].start( ); runner[1].setPriority(MIN_PRIORITY); runner[3].setPriority(MAX_PRIORITY);} public void run( ) { for(int i=0; i<1000000; i++); System.out.println(getName()+"執行緒的優先順序是"+getPriority()+"已計算完畢!"); } } |
執行結果:
測試程式4
l 教材642頁程式模擬一個有若干賬戶的銀行,隨機地生成在這些賬戶之間轉移錢款的交易。每一個賬戶有一個執行緒。在每一筆交易中,會從執行緒所服務的賬戶中隨機轉移一定數目的錢款到另一個隨機賬戶。
l 在Elipse環境下除錯教材642頁程式14-5、14-6,結合程式執行結果理解程式;
package synch; /** * This program shows how multiple threads can safely access a data structure. * @version 1.31 2015-06-21 * @author Cay Horstmann */ public class SynchBankTest { public static final int NACCOUNTS = 100; public static final double INITIAL_BALANCE = 1000; public static final double MAX_AMOUNT = 1000; public static final int DELAY = 10; public static void main(String[] args) { Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE); for (int i = 0; i < NACCOUNTS; i++) { int fromAccount = i; Runnable r = () -> { try { while (true) { int toAccount = (int) (bank.size() * Math.random()); double amount = MAX_AMOUNT * Math.random(); bank.transfer(fromAccount, toAccount, amount); Thread.sleep((int) (DELAY * Math.random())); } } catch (InterruptedException e) { } }; Thread t = new Thread(r); t.start(); } } }
package synch; import java.util.*; import java.util.concurrent.locks.*; /** * A bank with a number of bank accounts that uses locks for serializing access. * @version 1.30 2004-08-01 * @author Cay Horstmann */ public class Bank { private final double[] accounts; private Lock bankLock; private Condition sufficientFunds; /** * Constructs the bank. * @param n the number of accounts * @param initialBalance the initial balance for each account */ public Bank(int n, double initialBalance) { accounts = new double[n]; Arrays.fill(accounts, initialBalance); bankLock = new ReentrantLock(); sufficientFunds = bankLock.newCondition(); } /** * Transfers money from one account to another. * @param from the account to transfer from * @param to the account to transfer to * @param amount the amount to transfer */ public void transfer(int from, int to, double amount) throws InterruptedException { bankLock.lock(); try { while (accounts[from] < amount) sufficientFunds.await(); System.out.print(Thread.currentThread()); accounts[from] -= amount; System.out.printf(" %10.2f from %d to %d", amount, from, to); accounts[to] += amount; System.out.printf(" Total Balance: %10.2f%n", getTotalBalance()); sufficientFunds.signalAll(); } finally { bankLock.unlock(); } } /** * Gets the sum of all account balances. * @return the total balance */ public double getTotalBalance() { bankLock.lock(); try { double sum = 0; for (double a : accounts) sum += a; return sum; } finally { bankLock.unlock(); } } /** * Gets the number of accounts in the bank. * @return the number of accounts */ public int size() { return accounts.length; } }
綜合程式設計練習
程式設計練習1
- 設計一個使用者資訊採集程式,要求如下:
(1) 使用者資訊輸入介面如下圖所示:
(2) 使用者點選提交按鈕時,使用者輸入資訊顯示控制檯介面;
(3) 使用者點選重置按鈕後,清空使用者已輸入資訊;
(4) 點選視窗關閉,程式退出。
package demo; import java.awt.*; import javax.swing.*; public class Test { public static void main(String[] args) { EventQueue.invokeLater(() -> { Frame frame = new Frame(); frame.setTitle("Student Detail"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); }); } }
package demo; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Frame extends JFrame { private JPanel panel; private JPanel panel1; private JPanel panel2; private JPanel buttonPanel; private JComboBox<String> faceCombo; private JCheckBox Reading; private JCheckBox Singing; private JCheckBox Dancing; private JPanel panelDanXuan; private ButtonGroup option; private JRadioButton optionA; private JRadioButton optionB; private static final int DEFAULT_WITH = 800; private static final int DEFAULT_HEIGHT = 400; public Frame() { //框架a panel = new JPanel(); panel.setPreferredSize(new Dimension(200,160)); panel.setLayout(new GridLayout(2,4)); JLabel lab = new JLabel("Name:", JLabel.CENTER); final JTextField jt = new JTextField(); JLabel lab1 = new JLabel("Qualification:", JLabel.CENTER); faceCombo = new JComboBox<>(); faceCombo.addItem("Graduate"); faceCombo.addItem("Not graduated"); JLabel lab2 = new JLabel("Adress:", JLabel.CENTER); final JTextArea jt1 = new JTextArea(); JLabel lab3 = new JLabel("Hobby:", JLabel.CENTER); panel1 = new JPanel(); Reading = new JCheckBox("Reading"); Singing = new JCheckBox("Singing"); Dancing = new JCheckBox("Dancing "); //框架b panel2 = new JPanel(); panel2.setPreferredSize(new Dimension(200,160)); JLabel lab4 = new JLabel("Sex:", JLabel.CENTER); panelDanXuan = new JPanel(); option = new ButtonGroup(); optionA = new JRadioButton("Male"); optionB = new JRadioButton("Female"); //框架c buttonPanel = new JPanel(); buttonPanel.setPreferredSize(new Dimension(200,80)); JButton jButton1 = new JButton("Validate"); JButton jButton2 = new JButton("Reset"); panel.add(lab); panel.add(jt); panel.add(lab1); panel.add(faceCombo); panel.add(lab2); panel.add(jt1); panel.add(lab3); panel1.add(Reading); panel1.add(Singing); panel1.add(Dancing); panel1.setBorder(BorderFactory.createTitledBorder("")); panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); panel.add(panel1); panel2.add(lab4); option.add(optionA); option.add(optionB); panelDanXuan.add(optionA); panelDanXuan.add(optionB); panelDanXuan.setBorder(BorderFactory.createTitledBorder("")); panelDanXuan.setLayout(new BoxLayout(panelDanXuan, BoxLayout.Y_AXIS)); panel2.add(panelDanXuan); buttonPanel.add(jButton1); buttonPanel.add(jButton2); add(panel, BorderLayout.NORTH); add(panel2, BorderLayout.WEST); add(buttonPanel, BorderLayout.SOUTH); setSize(DEFAULT_WITH, DEFAULT_HEIGHT); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String Name = jt.getText(); if (Name != null) { System.out.println("Name:"+Name); } String m = faceCombo.getSelectedItem().toString(); System.out.println("Qualification:"+m); String Adress = jt1.getText(); if (Adress != null) { System.out.println("Adress:"+Adress); } System.out.println("Hobby:"); if(Reading.isSelected()) { System.out.println(Reading.getText()); } if(Singing.isSelected()) { System.out.println(Singing.getText()); } if(Dancing.isSelected()) { System.out.println(Dancing.getText()); } System.out.println("Sex:"); if(optionA.isSelected()) { System.out.println(optionA.getText()); } if(optionB.isSelected()) { System.out.println(optionB.getText()); } } }); jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jt.setText(""); jt1.setText(""); faceCombo.setSelectedItem("Graduate"); Reading.setSelected(false); Singing.setSelected(false); Dancing.setSelected(false); option.clearSelection(); } }); } }
2.建立兩個執行緒,每個執行緒按順序輸出5次“你好”,每個“你好”要標明來自哪個執行緒及其順序號。
package demo; class Lefthand extends Thread { public void run() { for (int i = 1; i <= 5; i++) { System.out.println(i+":a.你好!"); try { sleep(300); } catch (InterruptedException e) { System.out.println("Lefthand error."); } } } } class Righthand extends Thread { public void run() { for (int i = 1; i <= 5; i++) { System.out.println(i+":b.你好!"); try { sleep(300); } catch (InterruptedException e) { System.out.println("Righthand error."); } } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { left = new Lefthand(); right = new Righthand(); left.start(); right.start(); } }
3. 完善實驗十五 GUI綜合程式設計練習程式。
實驗總結:通過這周的學習知道了什麼是執行緒、 掌握了執行緒建立的兩種技術,實現Runnable介面。理解和掌握執行緒的優先順序屬性及排程方法,但沒有完全掌握。