版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
第Java實現(xiàn)新建有返回值的線程的示例詳解privatestaticfinallongserialVersionUID=2671056183299397274L;
privateJPanelcontentPane;
privateJTextAreathread1TextArea;
privateJTextAreathread2TextArea;
privateJTextAreathread3TextArea;
*Launchtheapplication.
publicstaticvoidmain(String[]args){
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Throwablee){
e.printStackTrace();
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
SynchronizedBankFrameframe=newSynchronizedBankFrame();
frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
*Createtheframe.
publicSynchronizedBankFrame(){
setTitle("新建有返回值的線程");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,450,300);
contentPane=newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(newBorderLayout(0,0));
JPanelbuttonPanel=newJPanel();
contentPane.add(buttonPanel,BorderLayout.SOUTH);
JButtonstartButton=newJButton("開始存錢");
startButton.setFont(newFont("微軟雅黑",Font.PLAIN,16));
startButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
do_button_actionPerformed(arg0);
buttonPanel.add(startButton);
JPanelprocessPanel=newJPanel();
contentPane.add(processPanel,BorderLayout.CENTER);
processPanel.setLayout(newGridLayout(0,3,5,5));
JPanelthread1Panel=newJPanel();
processPanel.add(thread1Panel);
thread1Panel.setLayout(newBorderLayout(0,0));
JLabelthread1Label=newJLabel("一號線程");
thread1Label.setFont(newFont("微軟雅黑",Font.PLAIN,16));
thread1Label.setHorizontalAlignment(SwingConstants.CENTER);
thread1Panel.add(thread1Label,BorderLayout.NORTH);
JScrollPanethread1ScrollPane=newJScrollPane();
thread1Panel.add(thread1ScrollPane,BorderLayout.CENTER);
thread1TextArea=newJTextArea();
thread1TextArea.setFont(newFont("微軟雅黑",Font.PLAIN,16));
thread1ScrollPane.setViewportView(thread1TextArea);
JPanelthread2Panel=newJPanel();
processPanel.add(thread2Panel);
thread2Panel.setLayout(newBorderLayout(0,0));
JLabelthread2Label=newJLabel("二號線程");
thread2Label.setFont(newFont("微軟雅黑",Font.PLAIN,16));
thread2Label.setHorizontalAlignment(SwingConstants.CENTER);
thread2Panel.add(thread2Label,BorderLayout.NORTH);
JScrollPanethread2ScrollPane=newJScrollPane();
thread2Panel.add(thread2ScrollPane,BorderLayout.CENTER);
thread2TextArea=newJTextArea();
thread2TextArea.setFont(newFont("微軟雅黑",Font.PLAIN,16));
thread2ScrollPane.setViewportView(thread2TextArea);
JPanelthread3Panel=newJPanel();
processPanel.add(thread3Panel);
thread3Panel.setLayout(newBorderLayout(0,0));
JLabelthread3Label=newJLabel("三號線程");
thread3Label.setFont(newFont("微軟雅黑",Font.PLAIN,16));
thread3Label.setHorizontalAlignment(SwingConstants.CENTER);
thread3Panel.add(thread3Label,BorderLayout.NORTH);
JScrollPanethread3ScrollPane=newJScrollPane();
thread3Panel.add(thread3ScrollPane,BorderLayout.CENTER);
thread3TextArea=newJTextArea();
thread3TextArea.setFont(newFont("微軟雅黑",Font.PLAIN,16));
thread3ScrollPane.setViewportView(thread3TextArea);
protectedvoiddo_button_actionPerformed(ActionEventarg0){
Bankbank=newBank();
Transfertransfer1=newTransfer(bank,thread1TextArea);//創(chuàng)建Transfer對象
Transfertransfer2=newTransfer(bank,thread2TextArea);//創(chuàng)建Transfer對象
FutureTaskIntegertask1=newFutureTaskInteger(transfer1);//創(chuàng)建FutureTask對象
FutureTaskIntegertask2=newFutureTaskInteger(transfer2);//創(chuàng)建FutureTask對象
Threadthread1=newThread(task1);//創(chuàng)建一號線程
Threadthread2=newThread(task2);//創(chuàng)建二號線程
thread1.start();//運行一號線程
thread2.start();//運行二號線程
try{
intthread1Result=task1.get();//獲得一號線程的計算結果
intthread2Result=task2.get();//獲得二號線程的計算結果
thread3TextArea.setText(thread3TextArea.getText()+"一號計算結果是:"+thread1Result+"\n");//更新三號線程文本域信息
thread3TextArea.setText(thread3TextArea.getText()+"二號計算結果是:"+thread2Result+"\n");//更新三號線程文本域信息
thread3TextArea.setText(thread3TextArea.getText()+"實際的金額是:"+(thread1Result+thread2Result-100)+"\n");//更新三號線程文本域信息
}catch(InterruptedExceptione){
e.printStackTrace();
}catch(ExecutionExceptione){
e.printStackTrace();
}
Bank
publicclassBank{
privatestaticThreadLocalIntegeraccount=newThreadLocalInteger(){
@Override
protectedIntegerinitialValue(){
return100;
publicvoiddeposit(intmoney){
account.set(account.get()+money);
publicintgetAccount(){
returnaccount.get();
}
Transfer
packagecom.xiaoxuzhu;
importjava.util.concurrent.Callable;
importjavax.swing.JTextArea;
*Description:
*@authorxiaoxuzhu
*@version1.0
*pre
*修改記錄:
*修改后版本修改人修改日期修改內(nèi)容
*2025/5/14.1xiaoxuzhu2025/5/14Create
*/pre
*@date2025/5/14
publicclassTransferimplementsCallableInteger{
privateBankbank;
privateJTextAreatextArea;
publicTransfer(Bankbank,JTextAreatextArea){//利用構造方法初始化變量
this.bank=bank;
this.textArea=textArea;
publicIntegercall
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年環(huán)境評估(土壤環(huán)境質(zhì)量評估)試題及答案
- 2025年中職(醫(yī)學檢驗)血常規(guī)檢測實務綜合測試題及答案
- 2025年大學(測繪科學與技術專業(yè))地理信息系統(tǒng)基礎試題及答案
- 2025年大學第四學年(工程項目融資)融資方案設計階段測試題及答案
- 2025年大學美術學(美術學概論)試題及答案
- 2025年大學安全教育(交通安全知識)試題及答案
- 2025年中職(市場開發(fā)實務)客戶開發(fā)流程階段測試試題及答案
- 2025年中職船舶工程技術(船舶建造工藝)試題及答案
- 2025年中職道路橋梁工程技術(路橋施工技術)試題及答案
- 2025年大學臨床醫(yī)學(臨床診療技術)試題及答案
- 服裝代運營協(xié)議書
- 對口升學考試綜合模擬試卷(第七版) 文化課綜合模擬試卷 參考答案
- 2025安徽省交通控股集團有限公司六安中心招聘收費協(xié)管員24人考試筆試參考題庫附答案解析
- 2025年移動式壓力容器充裝R2作業(yè)證考試練習題庫及答案
- 節(jié)能與新能源汽車技術路線圖2.0
- 保育員配合教學培訓工作指南
- FSSC22000 V6食品安全管理體系管理手冊及程序文件
- 2025年保安員理論考試100題(附答案)
- 3單元4 彩虹 課件 2025-2026學年統(tǒng)編版小學語文二年級上冊
- DBJT15-140-2018 廣東省市政基礎設施工程施工安全管理標準
- 肝豆狀核變性肝性腦病護理查房
評論
0/150
提交評論