如何执行线程?

时间:2014-01-18 21:19:50

标签: java multithreading

//Thread priorities

public class Thread_9 implements Runnable {

    static Thread th;
    public void run() 
    {
        th.currentThread();
        System.out.println(th.getName());
    }

    public static void main(String t[])
    {
        Thread_9 obj_1=new Thread_9();
        th=new Thread(obj_1,"Thread_1");
        th.start();
        Thread_9 obj_2=new Thread_9();      
        th=new Thread(obj_2,"Thread_2");
        th.start();
    }

}

输出: -

Thread_2
Thread_2

为什么输出不是

Thread_1
Thread_2

因为我两次调用start函数,首先调用名为“Thread_1”的obj_1然后调用obj_2 名称为“Thread_2”。

4 个答案:

答案 0 :(得分:4)

您正在使用static所以基本上只有一个Object引用与th相关联,即分配的最后一个引用。要获得单独的线程输出,您需要使用单独的线程实例

public class ThreadTest implements Runnable {

    public void run() {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String args[]) {

        Thread th = new Thread(new ThreadTest(), "Thread_1");
        th.start();
        Thread th2 = new Thread(new ThreadTest(), "Thread_2");
        th2.start();
    }
}

注意:在Thread_2首次出现

的情况下,仍可能出现竞争条件

答案 1 :(得分:0)

这就是你想要的:

public class Runnable_9 implements Runnable {

    public void run() 
    {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String t[])
    {
        Runnable_9 obj_1=new Runnable_9();
        Thread th1=new Thread(obj_1,"Thread_1");
        th1.start();
        Runnable_9 obj_2=new Runnable_9();      
        Thread th2=new Thread(obj_2,"Thread_2");
        th2.start();
    }
}

它可以满足您的期望:

$ java Runnable_9
Thread_1
Thread_2

除此之外,您的th.currentThread();语句不会执行任何事情;它返回对当前线程的引用,但是您将其丢弃。

答案 2 :(得分:0)

这个电话基本没有意义:

th.currentThread();

尝试在print语句中使用它:

System.out.println(th.currentThread().getName());

然后,即使你有一个静态线程,你也会得到输出

Thread_1
Thread_2

答案 3 :(得分:0)

试试这个:

public class Thread_9 implements Runnable {

    static Thread th;

    public void run() {
        th.currentThread();
        System.out.println(Thread.currentThread().getName()); // <<
    }

    public static void main(String t[]) throws InterruptedException {
        Thread_9 obj_1 = new Thread_9();
        th = new Thread(obj_1, "Thread_1");
        th.start();
        Thread_9 obj_2 = new Thread_9();
        th = new Thread(obj_2, "Thread_2");
        th.start();
    }
}

发生这种情况是因为在th.getName()的情况下,您只需获取引用的Thread的名称,该名称在“Thread_1”到达之前变为“Thread_2”。你可以查看:

public class Thread_9 implements Runnable {

    static Thread th;

    public void run() {
        th.currentThread();
        System.out.println(th.getName()); // << Your example code
    }

    public static void main(String t[]) throws InterruptedException {
        Thread_9 obj_1 = new Thread_9();
        th = new Thread(obj_1, "Thread_1");
        th.start();
        Thread.sleep(1000); // << Here
        Thread_9 obj_2 = new Thread_9();
        th = new Thread(obj_2, "Thread_2");
        th.start();
    }
}

输出:

Thread_1
Thread_2

在类而不是对象上调用静态方法也是一种很好的方式: 而不是:th.currentThread();使用此:Thread.currentThread();