线程示例,一段没一点意义的代码,自已也不知道拿来做什么,跟着书上抄的

来源:互联网 发布:七秀成女捏脸数据网盘 编辑:程序博客网 时间:2024/03/29 23:35

 //DeadLoop.java
//线程示例,一段没一点意义的代码,自已也不知道拿来做什么,跟着书上抄的
//2009-11-21

 

//<applet code=DeadLoop width=200 height=100>
//</applet>

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

 

public class  DeadLoop extends JApplet
{
 class ThreadClass extends Thread
 {
  int count1=0;
  int count2=0;
  boolean runFlag;
  public void run(){
   while (true)
   {
    try
    {
     sleep(100);//中断异常。
    }
    catch (InterruptedException e){}
   
   if (runFlag)
   {
    txt1.setText(Integer.toString(count1++));
   }
   else
    txt2.setText(Integer.toString(count2--));
   }
  }
 }
 
 JTextField txt1=new JTextField(10);
 JTextField txt2=new JTextField(10);
 JButton increase=new JButton("InCrease");
 JButton decrease=new JButton("DeCrease");
 ThreadClass addThread;
 ThreadClass subThread;

 class IncreaseListener implements ActionListener
 {
  public void actionPerformed(ActionEvent e){
   if (addThread==null)
   {
    addThread=new ThreadClass();
    addThread.runFlag=true;
    addThread.start();
   }
  }
 }

 class DecreaseListener implements ActionListener
 {
  public void actionPerformed(ActionEvent e){
   if (subThread==null)
   {
    subThread=new ThreadClass();
    subThread.runFlag=false;
    subThread.start();
   }
  }
 }

 public void init(){
  Container cp=getContentPane();
  cp.setLayout(new FlowLayout());
  cp.add(txt1);
  cp.add(txt2);
  cp.add(increase);
  cp.add(decrease);

  increase.addActionListener(new IncreaseListener());
  decrease.addActionListener(new DecreaseListener());
 }


}