awt线程

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;


public class myThread extends JFrame implements Runnable, ActionListener{
	Button bt01,bt02,bt03,bt04;
	TextArea are1,are2,are3,are4;
	Thread th1,th2,th3,th4;
	int number=100;
	myThread()
	{
		th1=new Thread(this);
		th2=new Thread(this);
		th3=new Thread(this);
		th4=new Thread(this);
		bt01=new Button("窗口1");
		bt02=new Button("窗口2");
		bt03=new Button("窗口3");
		bt04=new Button("窗口4");
		are1=new TextArea(10,5);
		are2=new TextArea(10,5);
		are3=new TextArea(10,5);
		are4=new TextArea(10,5);
		bt01.addActionListener(this);
		bt02.addActionListener(this);
		bt03.addActionListener(this);
		bt04.addActionListener(this);
		add(bt01);
		add(bt02);
		add(bt03);
		add(bt04);
		add(are1);
		add(are2);
		add(are3);
		add(are4);
		setLayout(new FlowLayout());
		setBounds(100, 100, 260, 300);
		setVisible(true);
		validate();
	}

	public synchronized void roll() {
		

	}
	@Override
	
	public void run() {
		// TODO Auto-generated method stub
		roll();
		while (number >= 0) {
			if (Thread.currentThread() == th1) {
				are1.append(number + "\n");
				number--;
				try {
					th1.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (Thread.currentThread() == th2) {
				are2.append(number + "\n");
				number--;
				try {
					th2.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (Thread.currentThread() == th3) {
				are3.append(number + "\n");
				number--;
				try {
					th3.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (Thread.currentThread() == th4) {
				are4.append(number + "\n");
				number--;
				try {
					th4.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			

		}
	
		
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource()==bt01)
		{
			if(!th1.isAlive())
			{
				th1=new Thread(this);
				number=100;
				th1.start();
			}
			else
			{
				are1.append("线程1进行着,不安全");
			}
			
		}
		if(arg0.getSource()==bt02)
		{
			if(!th2.isAlive())
			{
				th2=new Thread(this);
				number=100;
				th2.start();
			}
			else
			{
				are2.append("线程2进行着,不安全");
			}
			
		}
		if(arg0.getSource()==bt03)
		{
			if(!th3.isAlive())
			{
				th3=new Thread(this);
				number=100;
				th3.start();
			}
			else
			{
				are3.append("线程3进行着,不安全");
			}
			
		}
		if(arg0.getSource()==bt04)
		{
			if(!th4.isAlive())
			{
				th4=new Thread(this);
				number=100;
				th4.start();
			}
			else
			{
				are4.append("线程4进行着,不安全");
			}
			
		}
		
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值