多線程和標(biāo)準(zhǔn)輸入輸出流_第1頁
多線程和標(biāo)準(zhǔn)輸入輸出流_第2頁
多線程和標(biāo)準(zhǔn)輸入輸出流_第3頁
多線程和標(biāo)準(zhǔn)輸入輸出流_第4頁
多線程和標(biāo)準(zhǔn)輸入輸出流_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、實(shí) 驗(yàn) 報(bào) 告( 2015/ 2016 學(xué)年 第1學(xué)期)課程名稱JAVA語言程序設(shè)計(jì)實(shí)驗(yàn)名稱多線程程序設(shè)計(jì)實(shí)驗(yàn)時(shí)間2015年 12 月 1日指導(dǎo)單位軟件工程系指導(dǎo)教師 學(xué)院(系)專 業(yè)學(xué)生姓名班級(jí)學(xué)號(hào) PAGE 14實(shí)驗(yàn)名稱多線程程序設(shè)計(jì)指導(dǎo)教師楊健實(shí)驗(yàn)類型驗(yàn)證實(shí)驗(yàn)學(xué)時(shí)2實(shí)驗(yàn)時(shí)間2015/12/1實(shí)驗(yàn)?zāi)康恼莆認(rèn)ava中創(chuàng)建線程對(duì)象的方法。熟悉控制線程狀態(tài)的過程。熟悉線程同步機(jī)制。學(xué)會(huì)用多線程創(chuàng)建多任務(wù)的程序設(shè)計(jì)方法。二、實(shí)驗(yàn)環(huán)境(實(shí)驗(yàn)設(shè)備)Eclipse實(shí)驗(yàn)內(nèi)容(1)運(yùn)行 code1.txt 中的代碼,體會(huì)通過繼承Thread類構(gòu)造線程的特點(diǎn)。(2)編程完成:要求用實(shí)現(xiàn)Runnable接口方式

2、創(chuàng)建三個(gè)線程,每個(gè)線程的工作是在自己的界面隨機(jī)的顯示26個(gè)字母中的一個(gè)字母;可通過界面按鈕,臨時(shí)掛起或恢復(fù)線程的運(yùn)行。GUI顯示界面參考如下圖:GUI參考代碼見code2.txt隨機(jī)顯示字母的參考代碼見code3.txt實(shí)驗(yàn)代碼:package 多線程;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;/* * To change this license header, choose License Headers in Project Properties. * To change this t

3、emplate file, choose Tools | Templates * and open the template in the editor. */import java.awt.event.ActionListener;/* * * author acer */public class NewJFrame extends javax.swing.JFrame /* * Creates new form NewJFrame */ public NewJFrame() initComponents(); /* * This method is called from within t

4、he constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ SuppressWarnings(unchecked) / private void initComponents() jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax

5、.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jTextField3 = new javax.swing.JTextField(); jCheckBox1 = new javax.swing.JCheckBox(); jCheckBox2 = new javax.swing.JCheckBox(); jCheckBox3 = new javax.swing.JCheckBox(); setDefaultCloseOperation(

6、javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText(線程1:); jLabel2.setText(線程2:); jLabel3.setText(線程3:); jCheckBox1.setText(線程掛起); jCheckBox2.setText(線程掛起); jCheckBox3.setText(線程掛起); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane(); getContentPane().setLayout(lay

7、out); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(36, 36, 36) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComp

8、onent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILI

9、NG, layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addGro

10、up(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel1) .addGap(27, 27, 27) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(67, 67, 67) .addGroup(layout.createParallelGroup(javax.swing

11、.GroupLayout.Alignment.LEADING) .addComponent(jCheckBox1) .addComponent(jCheckBox2) .addComponent(jCheckBox3) .addContainerGap(115, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(25,

12、25, 25) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jCheckBox1) .addGap(28, 28, 2

13、8) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jCheckBox2) .addGap(30, 30, 30) .a

14、ddGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jCheckBox3) .addContainerGap(34, Short.

15、MAX_VALUE) ); pack(); / static class SimpleThread extends Thread public SimpleThread(String str) super(str);/調(diào)用其父類的構(gòu)造方法 public void run()/重寫run方法 for(int i=0;i+) Run(); try sleep(int)(Math.random()*1000); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace(); public sta

16、tic char suiJi() char suiJi=1; char ziMu=A,B,C,D,E,F,G,H,I,J,K,L,M,L,N, O,P,Q,R,S,T,U,V,W,X,Y,Z; boolean flag=new booleanziMu.length; for(int i=0;i5;i+) int s; do s=(int)(Math.random()*ziMu.length); while(flags); suiJi=ziMus; flags=true; return suiJi; SuppressWarnings(deprecation)public static void

17、Run() /System.out.println(#+SimpleThread.currentThread().getName(); if(SimpleThread.currentThread().getName().equals(線程1)&!jCheckBox1.isSelected() jTextField1.setText(suiJi()+); if(SimpleThread.currentThread().getName().equals(線程2)&!jCheckBox2.isSelected() jTextField2.setText(suiJi()+); if(SimpleThr

18、ead.currentThread().getName().equals(線程3)&!jCheckBox3.isSelected() jTextField3.setText(suiJi()+); /* * param args the command line arguments */ public static void main(String args) /* Set the Nimbus look and feel */ / /* If Nimbus (introduced in Java SE 6) is not available, stay with the default loo

19、k and feel. * For details see /javase/tutorial/uiswing/lookandfeel/plaf.html */ try for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels() if (Nimbus.equals(info.getName() javax.swing.UIManager.setLookAndFeel(info.getClassName(); break; catch (ClassNotFoun

20、dException ex) java.util.logging.Logger.getLogger(NewJFrame.class.getName().log(java.util.logging.Level.SEVERE, null, ex); catch (InstantiationException ex) java.util.logging.Logger.getLogger(NewJFrame.class.getName().log(java.util.logging.Level.SEVERE, null, ex); catch (IllegalAccessException ex) j

21、ava.util.logging.Logger.getLogger(NewJFrame.class.getName().log(java.util.logging.Level.SEVERE, null, ex); catch (javax.swing.UnsupportedLookAndFeelException ex) java.util.logging.Logger.getLogger(NewJFrame.class.getName().log(java.util.logging.Level.SEVERE, null, ex); / /* Create and display the fo

22、rm */ java.awt.EventQueue.invokeLater(new Runnable() public void run() new NewJFrame().setVisible(true); new SimpleThread(線程1).start(); new SimpleThread(線程2).start(); new SimpleThread(線程3).start(); ); / Variables declaration - do not modify private static javax.swing.JCheckBox jCheckBox1; private st

23、atic javax.swing.JCheckBox jCheckBox2; private static javax.swing.JCheckBox jCheckBox3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private static javax.swing.JTextField jTextField1; private static javax.swing.JTextField jTextField2; pr

24、ivate static javax.swing.JTextField jTextField3; / End of variables declaration 實(shí)驗(yàn)用例測(cè)試: 1、如圖,當(dāng)程序運(yùn)行時(shí),三條線程同時(shí)運(yùn)行,輸出字符 2、當(dāng)線程掛1起時(shí),線程1停止,線程一上輸出的字符不變 3、當(dāng)取消掛起時(shí),線程一繼續(xù)運(yùn)行四、實(shí)驗(yàn)小結(jié)(包括問題和解決方法、心得體會(huì)等) 一開始卡在線程掛起和取消掛起上面,最后通過資料的查閱和自己的四高解決了這個(gè)問題,也學(xué)到了許多,豐富了自己的知識(shí)面。五、指導(dǎo)教師評(píng)語成 績(jī)批閱人日 期實(shí)驗(yàn)名稱多線程程序設(shè)計(jì)指導(dǎo)教師楊健實(shí)驗(yàn)類型驗(yàn)證實(shí)驗(yàn)學(xué)時(shí)2實(shí)驗(yàn)時(shí)間2015/12/1實(shí)驗(yàn)?zāi)康耐ㄟ^本實(shí)驗(yàn)熟悉輸入流、輸出流的概念學(xué)會(huì)使用標(biāo)準(zhǔn)輸入/輸出流對(duì)標(biāo)準(zhǔn)設(shè)備進(jìn)行輸入和輸出;使用緩沖流進(jìn)行文件讀寫。學(xué)會(huì)設(shè)置并讀取程序運(yùn)行時(shí)的參數(shù)。理解輸入/輸出操作拋出的編譯性異常對(duì)象和異常的處理。二、實(shí)驗(yàn)環(huán)境(實(shí)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論