Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在一个小程序中加入两个线程类_Java_Multithreading_Applet_Awt - Fatal编程技术网

Java 如何在一个小程序中加入两个线程类

Java 如何在一个小程序中加入两个线程类,java,multithreading,applet,awt,Java,Multithreading,Applet,Awt,我有两个类的问题,每个类都有一个小程序有一个线程,但我想在一个小程序中运行这两个类,以便在最后有一个比赛的动画。我需要在一个类中启动两个线程 torturethread graphicsrace 重构——Tortoise和Hare应该是非applet、非GUI类。用一个GUI动画循环驱动它们。为小程序使用另一个类,该类显示乌龟和兔子对象的状态。而且,如果可能的话,我们需要使用applet,因为它是一种被抛弃的孤儿技术。如果您想继续这种设计,您是否考虑过CountDownLatch或信号量类?1)

我有两个类的问题,每个类都有一个小程序有一个线程,但我想在一个小程序中运行这两个类,以便在最后有一个比赛的动画。我需要在一个类中启动两个线程

torturethread
graphicsrace

重构——Tortoise和Hare应该是非applet、非GUI类。用一个GUI动画循环驱动它们。为小程序使用另一个类,该类显示乌龟和兔子对象的状态。而且,如果可能的话,我们需要使用applet,因为它是一种被抛弃的孤儿技术。如果您想继续这种设计,您是否考虑过CountDownLatch或信号量类?1)为什么要编写小程序?如果是老师指定的,请参考。2) 见和。3) 为什么要使用AWT?多看看。。。。放弃AWT组件以支持Swing的充分理由。4) 在源代码中只需要一行空白就可以了。
{
之后或
}
之前的空行通常也是多余的。5)
GraphicRace
-小程序通常没有
main
方法。或者,当在web浏览器中作为小程序运行时,将不会调用
main
方法。
public class TortuleThread extends Applet implements Runnable {
    public int y= 0;
    Thread thread;
     public void init(){
            thread = new Thread(this);
            thread.start();
            resize(800,700);
        }

     public void paint(Graphics g){
            g.setColor(Color.green);
            g.fillOval(80, 30+y, 50, 50);
            g.fillOval(88, 70+y, 30, 30);
            g.fillOval(70, 50+y, 20, 20);
            g.fillOval(120, 50+y, 20, 20);
            g.fillOval(85, 25+y, 20, 20);
            g.fillOval(105, 25+y, 20, 20);

            g.setColor(Color.BLACK);
            g.fillOval(92, 80+y, 6, 6);
            g.fillOval(105, 80+y, 6, 6);

            y=y+15;
        }
    public void run()
    {
    int i=0;
    System.out.println("start tortule");

    while(i<5)
    {
        repaint();
        try{
            Thread.sleep(5000);
            System.out.println("Tortule");
        }catch(InterruptedException ex)
        {   
        }
        i++;
    }
    System.out.println("end tortule");
}
}
public class HareThread extends Applet implements Runnable {
    public int y = 0;
    Thread thread;

    public void init() {
        thread = new Thread(this);
        thread.start();
        resize(800, 700);
    }

    public void paint(Graphics g) {
        g.setColor(Color.gray);
        g.fillOval(58, 3 + y, 8, 30);
        g.fillOval(68, 3 + y, 8, 30);
        g.fillRect(55, 30 + y, 20, 20);
        g.fillRect(40, 50 + y, 30, 30);
        g.setColor(Color.white);
        g.fillOval(60, 32 + y, 6, 6);
        g.fillOval(68, 32 + y, 6, 6);
        g.setColor(Color.red);
        g.fillOval(64, 40 + y, 6, 6);
        g.setColor(Color.gray);
        g.fillOval(30, 60 + y, 15, 15);

        y=y+15;
    }

    public void run() {
        int i = 0;
        System.out.println("start hare");
        while (i < 5) {
            repaint();
            try {
                Thread.sleep(2000);
                System.out.println("hare");
            } catch (InterruptedException ex) {
            }
            i++;
        }
        System.out.println("end the hare");
    }
}
public class GraphicRace extends Applet{

    public static void main (String [] args)
    {
    TortuleThread tortule = new TortuleThread();
    Thread hare = new Thread(new HareThread());
    tortule.start();
    hare.start();
    }
}