赛马的游戏:两个线程结束后(两匹马到终点)怎么判断谁先到?
int a;
a=a1;//这个应该放到哪个位置?a的值才是线程结束那一刻的值a1?
b=b1....又该在哪里判断两个值的大小
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.start();
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.start();
}
});class MyThread implements Runnable{
public void run(){
boolean flag=true;
while(flag){
if(x>=580){
flag=false;
Calendar c=new GregorianCalendar();int a1;
a1=c.getTimeInMillis();
}
x+=10;
}
int a;
a=a1;//这个应该放到哪个位置?a的值才是线程结束那一刻的值a1?
b=b1....又该在哪里判断两个值的大小
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.start();
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.start();
}
});class MyThread implements Runnable{
public void run(){
boolean flag=true;
while(flag){
if(x>=580){
flag=false;
Calendar c=new GregorianCalendar();int a1;
a1=c.getTimeInMillis();
}
x+=10;
}
解决方案 »
- 反编译遇到break MISSING_BLOCK_LABEL_369问题
- Java里getMethod方法的参数为什么要写成String.class?
- 哪位好心人能告诉我页面上的钟表功能是怎么实现的?
- 导入多个Excel文件后移动文件出错
- 同一页面有n个(n值不确定)同名的按钮,form怎么接收?
- 如何把字符串加到一个.txt文件末尾
- Java和C之间数据格式转换问题(做过和C程序交互的达人请帮忙看看)~~~
- 简单讲解用JMF技术在网页中用Applet直接抓取摄像头的影像的问题???
- j2sdk+redhat90 编译运行JAVA程序出问题,费解。
- 怎样禁止JBuilder 7里面的Project使用CVS
- httpunit如何实现HTTPS的访问
- String转成int就出错,不知道为什么,请大家帮忙。
1)a=a1;//这个应该放到run()方法中的最后的一条语句(或:线程快结束之前的最后一条语句). 因为:run()中代码执行完,就意味着线程结束了.
2)放在main()或第三个线程中,这个main或第三个线程等待那两个线程全执行结束后,才去进行 判断两个值的大小 (方法是:使用t1.joi()和t2.join()来等待线程t1,t2运行结束)
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class G3 extends JFrame{
private int x=20;
private int y=50;
private int x2=20;
private int y2=130;
long c0,d0;
JRadioButton one=new JRadioButton("1号");
JRadioButton two=new JRadioButton("2号");
JButton play=new JButton("PLAY");
JButton again=new JButton("AGAIN");
JOptionPane op=new JOptionPane();
ButtonGroup bg=new ButtonGroup();
public G3(String s){
super(s);
setBounds(100,100,730,630);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w){
int i=op.showConfirmDialog(G3.this,"tuichu","title",0,3);
if(i==0){dispose();}
}
});
add(play);
play.setBounds(320,530,70,60);
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.start();
MyThread2 mt2=new MyThread2();
Thread t2=new Thread(mt2);
t2.start();
//try{t.join();t2.join();}
//catch(InterruptedException i){System.out.println(c0);}
}}); add(again);
again.setBounds(400,530,70,60);
again.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a2){
x=20;x2=20;
}
}); add(one);
one.setBounds(30,320,60,25);
bg.add(one);
add(two);
two.setBounds(30,350,60,25);
bg.add(two);
setResizable(false);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
Image img=Toolkit.getDefaultToolkit().getImage("2.gif");
g.drawString("NUM1",10,50);
g.drawImage(img,x,y,100,60,null,this);
g.drawLine(140,40,140,330);
g.drawLine(680,40,680,330);
g.drawString("NUM2",10,130);
g.drawImage(img,x2,y2,100,60,null,this);
}
class MyThread implements Runnable{
public void run(){
boolean flag=true;
while(flag){
if(x>=580){
Calendar c=new GregorianCalendar();
flag=false;System.out.println("first");
System.out.println(c.getTimeInMillis());
long c3=c.getTimeInMillis();c0=c3;
System.out.println(c0);
}
x+=10;
try{ int a;
Random r=new Random();
a=r.nextInt(300);
Thread.sleep(a);
}
catch(Exception ep){ep.printStackTrace();}
}
}
} class MyThread2 implements Runnable{
public void run(){
boolean flag=true;
while(flag){
if(x2>=580){
flag=false;System.out.println("second");
Calendar c2=new GregorianCalendar();
System.out.println(c2.getTimeInMillis());
long d3=c2.getTimeInMillis();d0=d3;
System.out.println(d0);
}
x2+=10;
try{ int a;
Random r=new Random();
a=r.nextInt(300);
Thread.sleep(a);
}
catch(Exception ep){ep.printStackTrace();}
}
}//join();
}
public static void main(String[]args){
G3 g3=new G3("赛马");
//MyThread t1=new MyThread();g3.t1.join();MyThread2 t2=new MyThread2();t2.join();
}
}Thank you.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class G3 extends JFrame{
private int x=20;
private int y=50;
private int x2=20;
private int y2=130;
long c0,d0;
JRadioButton one=new JRadioButton("1号");
JRadioButton two=new JRadioButton("2号");
JButton play=new JButton("PLAY");
JButton again=new JButton("AGAIN");
JOptionPane op=new JOptionPane();
ButtonGroup bg=new ButtonGroup();
public G3(String s){
super(s);
setBounds(100,100,730,630);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w){
int i=op.showConfirmDialog(G3.this,"tuichu","title",0,3);
if(i==0){dispose();}
}
});
add(play);
play.setBounds(320,530,70,60);
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.start();
MyThread2 mt2=new MyThread2();
Thread t2=new Thread(mt2);
t2.start();
//try{t.join();t2.join();}
//catch(InterruptedException i){System.out.println(c0);}
}}); add(again);
again.setBounds(400,530,70,60);
again.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a2){
x=20;x2=20;
}
}); add(one);
one.setBounds(30,320,60,25);
bg.add(one);
add(two);
two.setBounds(30,350,60,25);
bg.add(two);
setResizable(false);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
Image img=Toolkit.getDefaultToolkit().getImage("2.gif");
g.drawString("NUM1",10,50);
g.drawImage(img,x,y,100,60,null,this);
g.drawLine(140,40,140,330);
g.drawLine(680,40,680,330);
g.drawString("NUM2",10,130);
g.drawImage(img,x2,y2,100,60,null,this);
}
class MyThread implements Runnable{
public void run(){
boolean flag=true;
while(flag){
if(x>=580){
Calendar c=new GregorianCalendar();
flag=false;System.out.println("first");
System.out.println(c.getTimeInMillis());
long c3=c.getTimeInMillis();c0=c3;
System.out.println(c0);
}
x+=10;
try{ int a;
Random r=new Random();
a=r.nextInt(300);
Thread.sleep(a);
}
catch(Exception ep){ep.printStackTrace();}
}
}
} class MyThread2 implements Runnable{
public void run(){
boolean flag=true;
while(flag){
if(x2>=580){
flag=false;System.out.println("second");
Calendar c2=new GregorianCalendar();
System.out.println(c2.getTimeInMillis());
long d3=c2.getTimeInMillis();d0=d3;
System.out.println(d0);
}
x2+=10;
try{ int a;
Random r=new Random();
a=r.nextInt(300);
Thread.sleep(a);
}
catch(Exception ep){ep.printStackTrace();}
}
}//join();
}
public static void main(String[]args){
G3 g3=new G3("赛马");
//MyThread t1=new MyThread();g3.t1.join();MyThread2 t2=new MyThread2();t2.join();
}
}Thank you.
第二种方法,设置一计数器(初始值为0),当计数器为2时,比较c0,d0的值并通知窗口。代码如下:import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicInteger;public class G3 extends JFrame{
private int x=20;
private int y=50;
private int x2=20;
private int y2=130;
long c0,d0;
JRadioButton one=new JRadioButton("1号");
JRadioButton two=new JRadioButton("2号");
JButton play=new JButton("PLAY");
JButton again=new JButton("AGAIN");
JOptionPane op=new JOptionPane();
ButtonGroup bg=new ButtonGroup();
public G3(String s){
super(s);
setBounds(100,100,730,630);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w){
int i=op.showConfirmDialog(G3.this,"tuichu","title",0,3);
if(i==0){dispose();}
}
});
add(play);
play.setBounds(320,530,70,60);
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
play.setEnable( false );
AtomicInteger counter = new AtomicInteger( 0 );
MyThread mt=new MyThread(counter, 2 );
Thread t=new Thread(mt);
t.start();
MyThread2 mt2=new MyThread2( counter, 2 );
Thread t2=new Thread(mt2);
t2.start();
//try{t.join();t2.join();}
//catch(InterruptedException i){System.out.println(c0);}
}
});
add(again);
again.setBounds(400,530,70,60);
again.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a2){
x=20;x2=20;
}
});
add(one);
one.setBounds(30,320,60,25);
bg.add(one);
add(two);
two.setBounds(30,350,60,25);
bg.add(two);
setResizable(false);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
Image img=Toolkit.getDefaultToolkit().getImage("2.gif");
g.drawString("NUM1",10,50);
g.drawImage(img,x,y,100,60,null,this);
g.drawLine(140,40,140,330);
g.drawLine(680,40,680,330);
g.drawString("NUM2",10,130);
g.drawImage(img,x2,y2,100,60,null,this);
}
private void decCounterAndNotifyWindow( AtomicInteger counter, int total ) {
if( total == counter.addAndGet( 1 ) ) {
play.setEnable( true );
}
}
class MyThread implements Runnable{
private AtomicInteger exitCounter_;
private int total_;
public MyThread( AtomicInteger exitCounter, int total ) {
exitCounter_ = exitCounter;
total_ = total_;
}
public void run(){
boolean flag=true;
while(flag){
if(x>=580){
Calendar c=new GregorianCalendar();
flag=false;System.out.println("first");
System.out.println(c.getTimeInMillis());
long c3=c.getTimeInMillis();c0=c3;
System.out.println(c0);
}
x+=10;
try{
int a;
Random r=new Random();
a=r.nextInt(300);
Thread.sleep(a);
}
catch(Exception ep){ep.printStackTrace();}
}
decCounterAndNotifyWindow( exitCounter_, total_ );
}
}
class MyThread2 implements Runnable{
private AtomicInteger exitCounter_;
private int total_;
public MyThread2( AtomicInteger exitCounter, int total ) {
exitCounter_ = exitCounter;
total_ = total_;
}
public void run(){
boolean flag=true;
while(flag){
if(x2>=580){
flag=false;System.out.println("second");
Calendar c2=new GregorianCalendar();
System.out.println(c2.getTimeInMillis());
long d3=c2.getTimeInMillis();d0=d3;
System.out.println(d0);
}
x2+=10;
try{
int a;
Random r=new Random();
a=r.nextInt(300);
Thread.sleep(a);
}
catch(Exception ep){ep.printStackTrace();}
}
decCounterAndNotifyWindow( exitCounter_, total_ );
}//join();
}
public static void main(String[]args){
G3 g3=new G3("赛马");
//MyThread t1=new MyThread();g3.t1.join();MyThread2 t2=new MyThread2();t2.join();
}
}