Java實現(xiàn)新建有返回值的線程的示例詳解_第1頁
Java實現(xiàn)新建有返回值的線程的示例詳解_第2頁
Java實現(xiàn)新建有返回值的線程的示例詳解_第3頁
Java實現(xiàn)新建有返回值的線程的示例詳解_第4頁
Java實現(xiàn)新建有返回值的線程的示例詳解_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論