目的
1. 掌握Runnable介面實作多執行緒的方法
2. 掌握Thread類實作多執行緒的用法
3. 掌握Java語言中多執行緒編程的基本方法
1. 執行緒接力(45分)
要求:撰寫一個應用程式,除了主執行緒外,還有三個執行緒:first、second和third,
first負責模擬一個紅色的按鈕從坐標(10,60)運動到(100,60);
second負責模擬一個綠色的按鈕從坐標(100,60)運動到(200,60);
third執行緒負責模擬一個藍色的按鈕從坐標(200,60)運動到(300,60),
2. 執行緒的控制(45分)
要求:撰寫一個程式,影片顯示文本域中的字串,在表單的南面添加三個按鈕,為程式添加執行緒控制功能,
點擊開始按鈕(startBtn),執行緒開始啟動,文字逐個顯示,并且將按鈕狀態改變為禁用(因為執行緒不能重復啟動)
點擊暫停按鈕(pauseBtn),執行緒暫停,文字顯示停止
點擊恢復按鈕(resumeBtn),執行緒恢復運行,文字繼續顯示
當執行緒執行完畢后,恢復開始按鈕的狀態為可用,
執行緒接力
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MoveButton extends Frame implements Runnable, ActionListener {
// 用Thread類宣告first,second,third三個執行緒物件
Thread first,second,third;
Button redButton, greenButton, blueButton, startButton;
int distance = 10;
MoveButton() {
//分別創建first,second,third三個執行緒,用當前視窗做為該執行緒的目標物件
first =new Thread(this);
second = new Thread(this);
third = new Thread(this);
redButton = new Button();
greenButton = new Button();
blueButton = new Button();
redButton.setBackground(Color.red);
greenButton.setBackground(Color.green);
blueButton.setBackground(Color.blue);
startButton = new Button("start");
startButton.addActionListener(this);
setLayout(null);
add(redButton);
redButton.setBounds(10, 60, 15, 15);
add(greenButton);
greenButton.setBounds(100, 60, 15, 15);
add(blueButton);
blueButton.setBounds(200, 60, 15, 15);
add(startButton);
startButton.setBounds(10, 100, 30, 30);
JLabel label1 = new JLabel("your name 不啦不啦不啊咯");
add(label1);
label1.setBounds(50, 105, 150, 25);
setTitle("執行緒接力");
setBounds(0, 0, 400, 200);
setVisible(true);
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
try {
// 分別啟動三個執行緒
first.start();
second.start();
third.start();
} catch (Exception exp) {
}
}
public void run() {
while (true) {
// 判斷當前占有CPU資源的執行緒是否是first
if (Thread.currentThread()==first) {
moveComponent(redButton);
try {
Thread.sleep(20);
} catch (Exception exp) {
}
}
// 判斷當前占有CPU資源的執行緒是否是second
if (Thread.currentThread()==second) {
moveComponent(greenButton);
try {
Thread.sleep(10);
} catch (Exception exp) {
}
}
// 判斷當前占有CPU資源的執行緒是否是third
if (Thread.currentThread()==third) {
moveComponent(blueButton);
try {
Thread.sleep(20);
} catch (Exception e) {
}
}
}
}
public synchronized void moveComponent(Component b) {
if (Thread.currentThread() == first) {
while (distance > 100 && distance <= 300)
try {
wait();
} catch (Exception exp) {
}
distance = distance + 1;
b.setLocation(distance, 60);
if (distance >= 100) {
b.setLocation(10, 60);
notifyAll();
}
}
if (Thread.currentThread() == second) {
while ((distance>10 && distance<100) && (distance>200 && distance<300) )
try {
wait();
} catch (Exception exp) {
}
distance = distance + 1;
b.setLocation(distance, 60);
if (distance > 200) {
b.setLocation(100, 60);
notifyAll();
}
}
if (Thread.currentThread() == third) {
while (distance<200)
try {
wait();
} catch (Exception exp) {
}
distance = distance + 1;
b.setLocation(distance, 60);
if (distance > 300) {
distance = 10;
b.setLocation(200, 60);
notifyAll();
}
}
}
public static void main(String[] args) {
new MoveButton().setLocationRelativeTo(null);
}
}
程式運行截圖
自己截圖粘貼進markdown
實驗總結
“執行緒接力”體現了Java多執行緒編程的執行緒切換特性,通過三個執行緒對一個組件進行移動,實作了多個執行緒間的協同作用,
執行緒的控制
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class RunnableDemo extends JFrame implements Runnable, ActionListener {
private JTextArea textArea; // 文本域組件
JLabel label;
Button startBtn, pauseBtn, resumeBtn;
Panel panel;
Thread thread;
boolean move = false;
// 影片顯示的文本字串
private String introduction = "現在大家已經對計算機很熟悉了,如今計算機的操作"
+ "系統可以同時執行多個任務,在聽歌的同時能夠打字、下載檔案,在聊天視窗打"
+ "字的時候,對方同時還能通過視頻看到你;聽到你,這一切都是使用多任務實作"
+ "的,Java語言使用多執行緒實作一個程式中的多個任務同時運行,程式員可以在程"
+ "序中執行多個執行緒,每一個執行緒完成一個功能,并與其他執行緒并發執行,這種機"
+ "制被稱為多執行緒,";
public static void main(String args[]) {
new RunnableDemo().setLocationRelativeTo(null); // 創建本類實體物件
}
public RunnableDemo() {
setTitle("執行緒的控制");
label = new JLabel("多執行緒簡介: your name 不啦不啦不啊咯"); // 標簽組件
getContentPane().add(label, BorderLayout.NORTH);// 添加標簽到表單
textArea = new JTextArea("\t"); // 初始化文本域組件
textArea.setBorder(new BevelBorder(BevelBorder.LOWERED));// 設定邊框
textArea.setLineWrap(true); // 設定自動折行
getContentPane().add(textArea, BorderLayout.CENTER);// 添加文本域組件到文本框
startBtn = new Button("開始");
pauseBtn = new Button("暫停");
resumeBtn = new Button("恢復");
startBtn.addActionListener(this);
pauseBtn.addActionListener(this);
resumeBtn.addActionListener(this);
panel = new Panel();
panel.add(startBtn);
panel.add(pauseBtn);
panel.add(resumeBtn);
getContentPane().add(panel, BorderLayout.SOUTH);
setBounds(0, 0, 383, 225); // 設定表單大小位置
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // 顯示表單
}
/**
* Runnable介面方法,是執行緒的執行方法
*/
@Override
public void run() {
textArea.setText("\t");
String[] intros = introduction.split(""); // 將字串分割為陣列
for (String ch : intros) { // ForEach遍歷字串陣列
while (!move) {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
textArea.append(ch); // 添加一個字符到文本域
try {
Thread.sleep(100); // 執行緒休眠0.1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
startBtn.setEnabled(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startBtn) {
thread =new Thread(this);
thread.start();
move=true;
startBtn.setEnabled(false);
} else if (e.getSource() == pauseBtn) {
move=false;
} else if (e.getSource() == resumeBtn) {
move=true;
synchronized (this) {
notifyAll();
}
}
}
}
程式運行截圖
自己截圖粘貼進markdown
實驗總結
這個實體體現了Java多執行緒編程的執行緒控制特性,通過執行緒的控制實作了影片效果的實時調整,
Java多執行緒編程非常重要,能夠提高程式運行效率,但同時也需要注意執行緒之間的協作和控制,避免死鎖,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/551989.html
標籤:其他
下一篇:返回列表