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进行着,不安全");
}
}
}
}
awt线程
最新推荐文章于 2023-11-06 21:52:56 发布