




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
线程的三个实验实验要求:(1)创建两个线程,每个线程的工作是在自己界面的TextField区域中从左到右动态地显示一个字符串,可通过界面按钮启动和中止每个线程的运行。/閬楃暀闂锛氱嚎绋嬪悓鏃剁潯鐪狅紵锛燂紵锛熺紦瀛樹竴涓嬪瓙杈撳嚭锛燂紵锛?import java.awt.*;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import javax.swing.*;import java.awt.event.*;public class ThreadTester1 extends JFrame private JButton runButton1,runButton2; private JButton interruptButton1,interruptButton2; private JTextArea threadTextArea1,threadTextArea2; private JTextField sleepTextField1,sleepTextField2; private ThreadTest threadtest1,threadtest2; private Container container; public ThreadTester1() container=this.getContentPane(); setnew(); layoutSet(); String string1=hello!my name is jj,string2=how are you?my name is zwc; threadtest1=new ThreadTest(threadTextArea1,string1,thread1); threadtest2=new ThreadTest(threadTextArea2,string2,thread2); setSize(400,200); setVisible(true); private void setnew() ButtonHandler buttonhandler=new ButtonHandler(); runButton1=new JButton(鍚姩); runButton1.addActionListener(buttonhandler); runButton2=new JButton(鍚姩); runButton2.addActionListener(buttonhandler); interruptButton1=new JButton(涓柇); interruptButton1.addActionListener(buttonhandler); interruptButton2=new JButton(涓柇); interruptButton2.addActionListener(buttonhandler); threadTextArea1=new JTextArea(); threadTextArea1.setBackground(Color.CYAN); threadTextArea2=new JTextArea(); threadTextArea2.setBackground(Color.CYAN); sleepTextField1=new JTextField(8); sleepTextField1.addActionListener(new TextFieldHandler1(); sleepTextField2=new JTextField(8); sleepTextField2.addActionListener(new TextFieldHandler2(); private void layoutSet() this.setLayout(new GridLayout(4,1); JPanel panel1=new JPanel(new FlowLayout(); panel1.setBackground(Color.GREEN); panel1.add(new JLabel(鐫湢鏃堕棿锛?); panel1.add(this.sleepTextField1); panel1.add(this.runButton1); panel1.add(erruptButton1); JPanel panel2=new JPanel(new FlowLayout(); panel2.setBackground(Color.GREEN); panel2.add(new JLabel(鐫湢鏃堕棿锛?); panel2.add(this.sleepTextField2); panel2.add(this.runButton2); panel2.add(erruptButton2); container.add(this.threadTextArea1); container.add(panel1); container.add(this.threadTextArea2); container.add(panel2); private class ButtonHandler implements ActionListener public void actionPerformed(ActionEvent event) if(event.getSource()=runButton1) try if(threadtest1.isAlive()return; threadtest1.start(); catch(IllegalThreadStateException illegalthreadexception) System.err.println(illegalthreadexception.toString(); else if(event.getSource() = runButton2) try threadtest2.start(); catch(IllegalThreadStateException illegalthreadexception) System.err.println(illegalthreadexception.toString(); else if(event.getSource() = interruptButton1) threadtest1.flag=false; else if(event.getSource() = interruptButton2) threadtest2.flag=false; private class TextFieldHandler1 implements ActionListener public void actionPerformed(ActionEvent event) threadtest1.sleeptime=Long.parseLong(sleepTextField1.getText(); private class TextFieldHandler2 implements ActionListener public void actionPerformed(ActionEvent event) threadtest2.sleeptime=Long.parseLong(sleepTextField2.getText(); private class ThreadTest extends Thread final long fixedspeed=500; public long sleeptime; private int count=0; public boolean flag=true; private String string; JTextArea textarea; public ThreadTest(JTextArea textarea,String string,String name) super(name); this.string=string; this.textarea=textarea; Override public void run() /sleeptime=fixedspeed; while(count=string.length()&flag) SwingUtilities.invokeLater( new Runnable() public void run() textarea.setText(string.substring(0, count+); ); try if(sleeptime=fixedspeed) Thread.sleep(fixedspeed); else Thread.sleep(sleeptime); sleeptime=fixedspeed; catch(InterruptedException interruptedException) System.err.println(interruptedException.toString(); public static void main(String args) ThreadTester1 threadtester=new ThreadTester1(); threadtester.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 实验要求:2)创建三个线程,每个线程的工作是在自己的界面随机地显示26个英文字母中的一个,可通过界面按钮,临时挂起或恢复各线程的运行。/* * To change this template, choose Tools | Templates * and open the template in the editor. */* * * author JJ */import java.awt.*;import javax.swing.*;import java.awt.event.*;public class ThreadTester2 extends JFrame protected String alphet=ABCDEFGHIJKLMNOPQRSTUVWXYZ; private JCheckBox blockCBox; /blockCBox1,blockCBox2,blockCBox3; private JLabel threadLabel;/threadLabel1,threadLabel2,threadLabel3; private Container container; Thread thread; private boolean suspend; public ThreadTester2() container=this.getContentPane(); container.setBackground(Color.GRAY); setnew(); layoutSet(); setSize(400,200); setVisible(true); private void setnew() blockCBox=new JCheckBox3; threadLabel=new JLabel3; thread=new Thread3; suspend=new boolean3; CheckBoxHandler checkboxhandler=new CheckBoxHandler(); for(int i=0;i3;i+) suspendi=false; blockCBoxi=new JCheckBox(绾跨+i+闃诲,false); blockCBoxi.addItemListener(checkboxhandler); threadLabeli=new JLabel(); threadLabeli.setBackground(Color.blue); threadi=new Thread(new RunnableOBJ(),Thread+i); threadi.start(); private class CheckBoxHandler implements ItemListener public void itemStateChanged(ItemEvent event) for(int i=0;i3;i+) if(event.getSource()=blockCBoxi) suspendi=!suspendi; if(!suspendi) synchronized(ThreadTester1.this) ThreadTester1.this.notifyAll(); else threadLabeli.setText(闃诲); return; private void layoutSet() this.setLayout(new GridLayout(3,2); for(int i=0;i3;i+) container.add(threadLabeli); container.add(blockCBoxi); private int getIndex(Thread t) for(int i=0;i3;i+) if(t=threadi)return i; return -1; private class RunnableOBJ implements Runnable public void run() final Thread currentthread=Thread.currentThread(); final int index; index=getIndex(currentthread); while(true) try Thread.sleep(500); synchronized (ThreadTester1.this)/閿佷綇涓荤嚎绋婽hreadTester1瀵硅薄锛屼唬鐮佸潡鐢簬鍐冲畾鏄惁鎸傝捣褰撳墠绾跨 while(suspendindex) ThreadTester1.this.wait(); catch(InterruptedException interruptedException) System.err.println(interruptedException.toString(); SwingUtilities.invokeLater ( new Runnable() public void run() int temp=(int)(java.lang.Math.random()*26); threadLabelindex.setText(绾跨+index+:+ThreadTester1.this.alphet.charAt(temp); );/end of SwingUtilities.invokeLater /end of run(); /end of private class RunnableOBJ implements Runnable public static void main(String args) ThreadTester2 threadtester1=new ThreadTester2(); threadtester1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 实验要求:(3)编写3个线程:各线程分别显示各自的运行时间,第一个线程每隔1秒运行一次,第二线程每隔5秒运行一次,第三个线程每隔10秒运行一次。/* * To change this template, choose Tools | Templates * and open the template in the editor. */package threadtester2;/* * * author JJ */import java.awt.*;import javax.swing.*;import java.util.Calendar;public class ThreadTest2 extends JFrame private Container container; Thread thread; private JLabel timelabel; Calendar date; public ThreadTest2() container=this.getContentPane(); container.setBackground(Color.lightGray); container.setLayout(new GridLayout(1,3); setnew(); layoutSet(); setSize(600,100); setVisible(true); private void setnew() timelabel=new JLabel3; thread=new Thread3; for(int i=0;i3;i+) timelabeli=new JLabel(); threadi=new Thread(new RunnableOBJ(); threadi.start(); private void layoutSet() for(int i=0;i3;i+) container.add(timelabeli); pr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国广电保定市2025秋招笔试行测题库及答案市场与服务类
- 中国广电张家界市2025秋招行业常识50题速记
- 厦门市中石化2025秋招笔试综合知识专练题库及答案
- 中国移动渭南市2025秋招计算机类专业追问清单及参考回答
- 国家能源海口市2025秋招法学类面试追问及参考回答
- 中国移动茂名市2025秋招面试无领导高频议题20例
- 驻马店市中石油2025秋招面试半结构化模拟题及答案炼油工艺技术岗
- 中国移动红河自治州2025秋招笔试行测经典题及答案
- 中国广电承德市2025秋招笔试行测题库及答案通信技术类
- 景德镇市中石油2025秋招面试半结构化模拟题及答案油田勘探开发岗
- 《AI创意壁纸设计之山水》课件
- TSG Z7004-2011 特种设备型式试验机构核准规则
- 回收垃圾培训课件
- 2025-2030中国钩针系列行业市场发展趋势与前景展望战略研究报告
- 司法确认调解协议(2025年版)
- 医疗器械直调管理制度
- (高清版)DBJ33∕T 1294-2023 建设工程造价指标采集分析标准
- 八年级英语上学期 选词填空解题方法及专项训练(解析版)
- 《永遇乐-京口北固亭怀古》课件
- 《幼儿舞蹈基础》 课件 项目八 蒙古族舞蹈
- 穴位按摩法操作评分标准
评论
0/150
提交评论