多线程接力

作者在 2011-12-27 15:12:55 发布以下内容
import java.awt.*;
import java.awt.event.*;

public class DoubleThreadFrame extends Frame implements Runnable,ActionListener{
        Thread first,second,third;//创建两个线程
        int distance=10;
        Button redButton,greenButton,blueButton,startButton;
     public DoubleThreadFrame() {
           first=new  Thread(this); // 创建first线程,当前窗口作为该线程的目标对象  
            second=new  Thread(this);
            third=new  Thread(this);
            
            redButton=new Button();
            greenButton=new Button();
            startButton=new Button();
            blueButton=new Button();
            //设置按钮颜色
            redButton.setBackground(Color.red);
            greenButton.setBackground(Color.green);
            blueButton.setBackground(Color.BLUE);
            
            startButton=new Button("start");
            startButton.addActionListener(this);
            
            this.setLayout(null);
            this.add(redButton);
            redButton.setBounds(10,60,15,15);
          
            this.add(greenButton);
            greenButton.setBounds(100,60,15,15);
            
            this.add(blueButton);
            blueButton.setBounds(200,60,15,15);
            
            this.add(startButton);
            startButton.setBounds(10,100,30,30);
              
            this.setBounds(0,0,300,200);
            this.setVisible(true);
            this.validate();
            addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
     }
    
     public void actionPerformed(ActionEvent e){
         try {
             first.start();
             second.start();
             third.start();
         }
         catch (Exception ex) {
         }
     }
     public void run(){
         while(true){
             if(Thread.currentThread()==third){
                 moveComponent(blueButton);
                 try {
                     Thread.sleep(10);
                 }
                 catch (Exception ex) {
                 }
             }
            
             if(Thread.currentThread()==second){
                 moveComponent(greenButton);
                 try {
                     Thread.sleep(20);
                 }
                 catch (Exception ex) {
                 }
             }
            
             if(Thread.currentThread()==first){
                 moveComponent(redButton);
                 try {
                     Thread.sleep(30);
                 }
                 catch (Exception ex) {
                 }
             }
            
            
         }        
     }
     public synchronized void moveComponent(Component b){
         if(Thread.currentThread()==first){
             while(!(distance>=10&&distance<=100))//距离区间
                 try {
                     wait();
                 }
                 catch (Exception ex) {
                 }
                 distance++;
                 b.setLocation(distance,60);
//System.out.println("1");
                 if(distance>=100){
                     b.setLocation(10,60);//返回起始点
                     notifyAll();
                 }
             }
            
             if(Thread.currentThread()==second){
                 while(!(distance>100&&distance<=200))//距离区间
                 try {
                     wait();
                 }
                 catch (Exception ex) {
                 }
                 distance++;
                 b.setLocation(distance,60);
//System.out.println("2");
                 if(distance>200){
        //             distance=10;//返回原点
                     b.setLocation(100,60);//返回起始点
                     notifyAll();
                 }
             }
            
             if(Thread.currentThread()==third){
                 while(!(distance>200&&distance<=300))//距离区间
                 try {
                     wait();
                 }
                 catch (Exception ex) {
                 }
                
                 distance++;
                 b.setLocation(distance,60);
//System.out.println("3");
                 if(distance>300){
                     distance=10;//返回原点
                     b.setLocation(200,60);
                     notifyAll();
                 }
             }

     }
}
 


public class DoubleThread {
    
    public static void main(String[] args) {
        new DoubleThreadFrame();
    }
}
 
java codes | 阅读 2628 次
文章评论,共0条
游客请输入验证码
浏览17073次
最新评论