Java挂起设置

Python011

Java挂起设置,第1张

import java.applet.*

import javax.swing.*

import java.awt.*

import java.awt.event.*

import java.util.*

public class UseRunnable extends Applet implements ActionListener{

JButton b1,b2,b3,b11,b22,b33

JTextField t1,t2,t3

Random rand=new Random(47)

int[] a={65,97} Thread f1,f2,f3

boolean a1,a2,a3

public void init( ){

setLayout(new FlowLayout( ))

b1=new JButton("激活1") b11=new JButton("挂起1") t1=new JTextField(5)t1.setEditable(false)

add(b1) add(b11) add(t1)

b2=new JButton("激活2") b22=new JButton("挂起2") t2=new JTextField(5)t2.setEditable(false)

add(b2)add(b22)add(t2)

b3=new JButton("激活3") b33=new JButton("挂起3") t3=new JTextField(5)t3.setEditable(false)

add(b3)add(b33)add(t3)

setBackground(Color.LIGHT_GRAY)

resize(250,110)

b1.addActionListener(this)b11.addActionListener(this)

b2.addActionListener(this)b22.addActionListener(this)

b3.addActionListener(this)b33.addActionListener(this)

}

class A implements Runnable{

public void run( ){

while(true){

while(a1){getstop( )}

t1.setText(gett( ))

try{Thread.sleep((int)(Math.random()*500))}catch(Exception e){}

}

}

}

class B implements Runnable{

public void run( ){

while(true){

while(a2){getstop( )}

t2.setText(gett( ))

try{Thread.sleep((int)(Math.random( )*500))}catch(Exception e){}

}

}

}

class C implements Runnable{

public void run( ){

while(true){

while(a3){getstop( )}

t3.setText(gett( ))

try{Thread.sleep((int)(Math.random( )*500))}catch(Exception e){}

}

}

}

public String gett( ){

int i1=a[rand.nextInt(2)]

return String.valueOf((char)(rand.nextInt(26)+i1))

}

public void start( ){

f1=new Thread(new A( ))

f2=new Thread(new B( ))

f3=new Thread(new C( ))

f1.start( )f2.start( )f3.start( )

}

public synchronized void getstop( ){try{wait( )}catch(Exception e1){}}

public synchronized void getstart( ){notifyAll( )}

public void actionPerformed(ActionEvent e){

if(e.getSource( )==b1){a1=falsegetstart( )}

if(e.getSource( )==b11){a1=true}

if(e.getSource( )==b2){a2=falsegetstart( )}

if(e.getSource( )==b22){a2=true}

if(e.getSource( )==b3){a3=falsegetstart( )}

if(e.getSource( )==b33){a3=true}

}

}

要利用循环啊!

sleep和wait的区别:

1、sleep的意思是:睡,睡觉,睡眠

2、wait的意思是:等候,推迟,延缓等待,耽搁,伺候用餐。

拓展资料

sleep的用法

1、They were exhausted from lack of sleep

由于缺乏睡眠,他们非常疲惫。

2、During the car journey, the baby slept

坐车来的路上,宝宝睡着了。

3、I think he may be ready for a sleep soon.

我想他也许很快就要睡一觉了。

4、I can't get to sleep with all that singing.

那些歌声搅得我无法入睡。

5、I didn't lose too much sleep over that investigation.

我并不太担心那个调查。

wait

1、I walk to a street corner and wait for the school bus

我走到街角等校车。

2、There'll be a car waiting for you

会有辆汽车等你。

3、I want to talk to you, but it can wait

我想和你谈谈,但可以晚点再说。

4、If you think this all sounds very exciting, just wait until you read the book

如果你觉得所有这些听起来令人兴奋,那就等着去读这本书吧。

5、'Wait a minute!' he broke in. 'This is not giving her a fair hearing!'

“等一下,”他插嘴说,“这没有给她一个公平的解释机会!”