Java介紹多線程計(jì)算階乘實(shí)現(xiàn)方法
代碼運(yùn)行結(jié)果如下:

輸入n的值之后,就可以點(diǎn)擊開(kāi)始計(jì)算,計(jì)算過(guò)程中可以暫停計(jì)算,也可以停止計(jì)算

這是幾種線程的操作:
1.sleep方法,線程按時(shí)間睡眠,到時(shí)間恢復(fù)。
2.suspend/resume,暫停/繼續(xù)方法。Java多線程廢棄方法。資源獨(dú)占,容易發(fā)生死鎖,臟數(shù)據(jù)。
3.stop,停止方法,Java多線程廢棄方法,線程不安全。
4.wait方法,使得當(dāng)前線程立刻停止運(yùn)行,處于等待狀態(tài)(WAIT),并將當(dāng)前線程置入鎖對(duì)象的等待隊(duì)列中,直到被通知(notify)或被中斷為止。
5.notify方法,喚醒處于等待狀態(tài)的線程
代碼如下:
import java.awt.BorderLayout;
import java.awt.ScrollPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Scrollable;
import alg.Calculate;
public class ThreadUI {
private JTextField textField;
private JTextArea consoleArea;
private Calculate cal;
private Thread thread;
public ThreadUI() {
JFrame frame = new JFrame("階乘計(jì)算所用時(shí)間統(tǒng)計(jì)");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 北側(cè),加操作欄
frame.setLayout(new BorderLayout());
JPanel northPan = new JPanel();
JLabel label = new JLabel("請(qǐng)輸入n的值:");
textField = new JTextField(5);
JButton stratBtn = new JButton("開(kāi)始計(jì)算");
stratAction(stratBtn);
JButton pauseBtn = new JButton("暫停計(jì)算");
pauseAction(pauseBtn);
JButton stopBtn = new JButton("停止計(jì)算");
stopAction(stopBtn);
northPan.add(label);
northPan.add(textField);
northPan.add(stratBtn);
northPan.add(pauseBtn);
northPan.add(stopBtn);
frame.add(northPan, BorderLayout.NORTH);
// 中間,記錄計(jì)算過(guò)程與結(jié)果
consoleArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(consoleArea);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
// 停止計(jì)算的事件
private void stopAction(JButton stopBtn) {
stopBtn.addActionListener((e) -> {
cal.stop();
//thread.stop();
});
}
// 暫停計(jì)算的事件
private void pauseAction(JButton pauseBtn) {
pauseBtn.addActionListener((e) -> {
String pauseText = pauseBtn.getText();
if (pauseText.equals("暫停計(jì)算")) {
cal.suspend();
//thread.suspend();
pauseBtn.setText("繼續(xù)計(jì)算");
} else {
cal.resume();
//thread.resume();
pauseBtn.setText("暫停計(jì)算");
}
});
}
// 開(kāi)始計(jì)算的事件
private void stratAction(JButton button) {
button.addActionListener((e) -> {
consoleArea.setText(null);
String input = textField.getText();
if(input==null || input.equals(""))
{
consoleArea.setText("請(qǐng)先輸入您要計(jì)算的階乘值");
return;
}
int n = Integer.valueOf(input);
cal = new Calculate(n, (content) -> {
consoleArea.append(content + "\n");
});
// thread = new Thread(cal);
// thread.start();
cal.start();
});
}
public static void main(String[] args) {
new ThreadUI();
}
}import java.math.BigInteger;
import ui.Logable;
public class Calculate extends Thread /*implements Runnable*/{
private int n;
private Logable logable;
public Calculate(int n,Logable log) {
this.n=n;
this.logable=log;
}
private void caculate() throws InterruptedException {
BigInteger res=BigInteger.valueOf(1);
BigInteger sum=BigInteger.valueOf(0);
StringBuilder sb = new StringBuilder();
for(int i=1;i<=n;i++) {
res=res.multiply(BigInteger.valueOf(i));
sum=sum.add(res);
sb.append((i==1 ? "":"+")+i+"!");
logable.log(sb.toString()+"="+sum);
try {
Thread.sleep((int)(Math.random()*500+500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
try {
caculate();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}今天就到這里啦,我們下次見(jiàn)哦
到此這篇關(guān)于Java介紹多線程計(jì)算階乘實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Java多線程計(jì)算階乘內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Security使用數(shù)據(jù)庫(kù)登錄認(rèn)證授權(quán)
本文主要介紹了Spring?Security使用數(shù)據(jù)庫(kù)登錄認(rèn)證授權(quán),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Java基礎(chǔ)知識(shí)精通注釋與數(shù)據(jù)類型及常量與變量
本文給大家介紹了Java的注釋與數(shù)據(jù)類型和常量變量,這些都是最基礎(chǔ)的知識(shí),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
如何用Java來(lái)進(jìn)行文件切割和簡(jiǎn)單的內(nèi)容過(guò)濾的實(shí)現(xiàn)
這篇文章主要介紹了如何用Java來(lái)進(jìn)行文件切割和簡(jiǎn)單的內(nèi)容過(guò)濾的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
try-with-resource優(yōu)雅關(guān)閉io流的方法
這篇文章主要給大家介紹了關(guān)于try-with-resource優(yōu)雅關(guān)閉io流的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
redis scan命令導(dǎo)致redis連接耗盡,線程上鎖的解決
這篇文章主要介紹了redis scan命令導(dǎo)致redis連接耗盡,線程上鎖的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Spring Web MVC和Hibernate的集成配置詳解
這篇文章主要介紹了Spring Web MVC和Hibernate的集成配置詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Java針對(duì)ArrayList自定義排序的2種實(shí)現(xiàn)方法
這篇文章主要介紹了Java針對(duì)ArrayList自定義排序的2種實(shí)現(xiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了Java操作ArrayList自定義排序的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01

