Java中线程的初始化

时间:2015-09-01 07:12:30

标签: java multithreading

Thread t=new Thread(nr);

初始化时传递的参数nr将在线程类中捕获。

这个和非参数化初始化如Thread t= new Thread();之间有什么区别?

4 个答案:

答案 0 :(得分:2)

使用Runnable调用run()的构造函数。

第二个构造函数将在Thread上调用run()来启动它。默认操作无效。这仅在您对Thread进行子类化并覆盖run()时才有用,但这通常被认为是一个坏主意,因为如果您这样做,则封装很差。

为什么你不应该对子类进行子类化的一个例子。

public class Main {
    public String getName() {
        return "Main";
    }

    public String getId() {
        return "My-id";
    }

    public String getState() {
        return "Good";
    }

    public void printMe() {
        new Thread() {
            @Override
            public void run() {
                System.out.println("name: " + getName() + ", id:" + getId() + ", state:" + getState());
            }
        }.start();
    }

    public static void main(String[] args) {
        new Main().printMe();
    }
}

印刷可能令人惊讶

name: Thread-0, id:11, state:RUNNABLE

你可能想知道为什么Thread允许你对它进行子类化。最可能的解释是Thread在最初的Java 1.0中,当时他们没有像在Java 1.2(1998)等更高版本中那样使用接口和封装。例如收藏库。

答案 1 :(得分:0)

如果使用Thread t = new Thread();,则不会实现Runnable接口以运行该线程。在Thread中,这是运行函数:

private Runnable target;
   ...
@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

因此,如果您未设置runnable,则使用构造函数target将为null。

答案 2 :(得分:0)

  

创建线程的两种方法

    Thread thread = new Thread();
    thread.start();
    // This doesn't specify any code for the thread to execute. 
    // It will stop again right away.

    Thread threadname = new Thread("myThread");
    threadname.start();
    // This Allocates a new Thread object.
    // myThread the name of the new thread 


    //There are two ways to create a Thread

    //1) WAY
    //runnable1 and runnable2 are just two different objects of the class 
    //that implements the Runnable interfaces which have that run() method.
    //When you call runnable1.run() you are executing it in the current thread.
    ClassOne runnable1 = new ClassOne();
    ClassTwo runnable2 = new ClassTwo();

    //thread1 and thread2 are the objects of the class Thread. 
    //When you call thread1.start(), it starts a new thread and calls the run()
    //method of runnable1 internally to execute it within that new thread.
    Thread thread1 = new Thread(runnable1);
    Thread thread2 = new Thread(runnable2);
    thread1.start();
    thread2.start();

    //2) WAY 
    //Create a subclass of Thread and override the run() method. 
    //The run() method is what is executed by the thread after you call start(). 
    class MyThread extends Thread 
    {
       public void run()
       {
           System.out.println("MyThread running");
       }
    }

    MyThread myThread = new MyThread();
    myThread.start();

答案 3 :(得分:0)

无论哪种方式,您都将创建一个新线程。为了在新线程中执行任务,您必须覆盖run()方法 主要有三种创建新线程的方法:
(1)在你的班级中实现Runnable接口 (2)扩展Thread类 (3)使用内部阶级
推荐的方法是第一个使用Runnable接口的方法。因为如果你扩展Thread类,那么你将无法扩展任何其他类,但是如果你使用接口你可以实现许多接口,你也可以扩展其他类。

你的问题
1)Thread t=new Thread(nr);
在这里,您传递一个实现可运行接口的类对象 2)Thread t= new Thread();
此代码不会执行任何操作,因为它不会覆盖run()方法。但您可以通过将runnable接口实现为参数来使用此代码
这是代码,演示了创建线程的不同方法。

public class ThreadMain {

    public static void main(String[] args) {
        //Main thread
        System.out.println(Thread.currentThread().getName());

        //Task 1 thread
        //Using runnable
        Thread task1 = new Thread(new Task1());
        task1.start();

        //Task 2 thread
        //By extending Thread class
        Thread task2 = new Task2();
        task2.start();

        //Other way is using inner class
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });
        thread.start();
    }
}

class Task1 implements Runnable {

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

class Task2 extends Thread {

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