当前位置 : 主页 > 编程语言 > java >

多线程测试第一种方法

来源:互联网 收集:自由互联 发布时间:2021-06-28
用继承Thread的方法 class A extends Thread{ private int i = 0; @Override public void run() { System.out.println("第一个线程:"); for (int n =1;n=10;n++) { i=i+1; System.out.println(i); } }}class B extends Thread{ private int i =
用继承Thread的方法
class A extends Thread{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第一个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i+1;
            System.out.println(i);
        }
    }
}

class B extends Thread{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第二个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i+1;
            System.out.println(i);
        }
    }
}

class C extends Thread{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第三个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i-1;
            System.out.println(i);
        }
    }
}

class D extends Thread{
    private int i = 0;

    @Override
    public void run() {
        System.out.println("第四个线程:");
        for (int n =1;n<=10;n++)
        {
            i=i-1;
            System.out.println(i);
        }
    }
}



public class manu  {
    public static void main(String[] args) throws Exception {
        A a = new A();
        B b = new B();
        C c = new C();
        D d = new D();
        a.start();
        a.join();
        b.start();
        b.join();
        c.start();
        c.join();
        d.start();
    }
}
网友评论