您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 多线程和标准输入输出流
实验报告(2015/2016学年第1学期)课程名称JAVA语言程序设计实验名称多线程程序设计实验时间2015年12月1日指导单位软件工程系指导教师学院(系)专业学生姓名班级学号1实验名称多线程程序设计指导教师杨健实验类型验证实验学时2实验时间2015/12/1一、实验目的(1)掌握Java中创建线程对象的方法。(2)熟悉控制线程状态的过程。(3)熟悉线程同步机制。(4)学会用多线程创建多任务的程序设计方法。二、实验环境(实验设备)Eclipse三、实验内容(1)运行code1.txt中的代码,体会通过继承Thread类构造线程的特点。(2)编程完成:要求用实现Runnable接口方式创建三个线程,每个线程的工作是在自己的界面随机的显示26个字母中的一个字母;可通过界面按钮,临时挂起或恢复线程的运行。GUI显示界面参考如下图:GUI参考代码见code2.txt随机显示字母的参考代码见code3.txt实验代码:package多线程;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;/**Tochangethislicenseheader,chooseLicenseHeadersinProjectProperties.*Tochangethistemplatefile,chooseTools|Templates*andopenthetemplateintheeditor.*/importjava.awt.event.ActionListener;2/****@authoracer*/publicclassNewJFrameextendsjavax.swing.JFrame{/***CreatesnewformNewJFrame*/publicNewJFrame(){initComponents();}/***Thismethodiscalledfromwithintheconstructortoinitializetheform.*WARNING:DoNOTmodifythiscode.Thecontentofthismethodisalways*regeneratedbytheFormEditor.*/@SuppressWarnings(unchecked)//editor-folddefaultstate=collapseddesc=GeneratedCodeprivatevoidinitComponents(){jLabel1=newjavax.swing.JLabel();jLabel2=newjavax.swing.JLabel();jLabel3=newjavax.swing.JLabel();jTextField1=newjavax.swing.JTextField();jTextField2=newjavax.swing.JTextField();jTextField3=newjavax.swing.JTextField();jCheckBox1=newjavax.swing.JCheckBox();jCheckBox2=newjavax.swing.JCheckBox();jCheckBox3=newjavax.swing.JCheckBox();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jLabel1.setText(线程1:);jLabel2.setText(线程2:);jLabel3.setText(线程3:);jCheckBox1.setText(线程挂起);3jCheckBox2.setText(线程挂起);jCheckBox3.setText(线程挂起);javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);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().addComponent(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.TRAILING,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)).addGroup(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.GroupLayout.Alignment.LEADING).addComponent(jCheckBox1).addComponent(jCheckBox2).addComponent(jCheckBox3)).addContainerGap(115,Short.MAX_VALUE)));4layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(25,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,28).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).addGroup(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.MAX_VALUE)));pack();}///editor-foldstaticclassSimpleThreadextendsThread{publicSimpleThread(Stringstr){super(str);//调用其父类的构造方法}publicvoidrun(){//重写run方法for(inti=0;;i++){Run();5try{sleep((int)(Math.random()*1000));}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}publicstaticcharsuiJi(){charsuiJi='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=newboolean[ziMu.length];for(inti=0;i5;i++){ints;do{s=(int)(Math.random()*ziMu.length);}while(flag[s]);suiJi=ziMu[s];flag[s]=true;}returnsuiJi;}@SuppressWarnings(deprecation)publicstaticvoidRun(){//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()+
本文标题:多线程和标准输入输出流
链接地址:https://www.777doc.com/doc-5607914 .html