如果我们在 start 方法而不是 运行 方法中执行整个 Thread 功能会发生什么?
What will happen if we do entire Thread functionality in start method instead of run method?
如果我们在 start 方法而不是 运行 方法中执行整个 Thread 功能会发生什么?下面的代码 运行s 与 运行 如果我添加代码的方式相同在 运行 方法中..
public class RunMethodTest extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Abc abc=new Abc();
abc.start();
}
}
class Abc extends Thread
{
@Override
public synchronized void start() {
super.start();
for(int i=0;i<10;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread" + " " + i);
}
}
}
对其进行测试:让您的代码告诉您
在哪个线程中调用它
- 通过
Thread.currentThread()
获取当前线程
- 然后通过
getId()
和getName()
获取当前线程的id和名称
public class ThreadTest {
public static void main(String[] args) {
Thread currentThread = Thread.currentThread();
System.out.printf("Main, Which Thread: %s, %d%n",
currentThread.getName(),
currentThread.getId());
Abc abc = new Abc();
abc.start();
}
}
class Abc extends Thread {
@Override
public synchronized void start() {
super.start();
Thread currentThread = Thread.currentThread();
System.out.printf("Start, Which Thread: %s, %d%n",
currentThread.getName(),
currentThread.getId());
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread" + " " + i);
}
}
@Override
public void run() {
super.run();
Thread currentThread = Thread.currentThread();
System.out.printf("Run, Which Thread: %s, %d%n",
currentThread.getName(),
currentThread.getId());
}
}
对我来说这个returns:
Main, Which Thread: main, 1
Start, Which Thread: main, 1
Run, Which Thread: Thread-0, 9
Thread 0
Thread 1
Thread 2
Thread 3
....
这证明您的 Abc 的 start 方法(正如预期的那样)在与调用代码相同的线程中被调用,而不是在新线程中被调用,并且只有 run()
方法中的代码被调用一个新线程。如上所述,您几乎不需要或不想扩展 Thread,而是实现 Runnable 或 Callable。
如果我们在 start 方法而不是 运行 方法中执行整个 Thread 功能会发生什么?下面的代码 运行s 与 运行 如果我添加代码的方式相同在 运行 方法中..
public class RunMethodTest extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Abc abc=new Abc();
abc.start();
}
}
class Abc extends Thread
{
@Override
public synchronized void start() {
super.start();
for(int i=0;i<10;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread" + " " + i);
}
}
}
对其进行测试:让您的代码告诉您
在哪个线程中调用它- 通过
Thread.currentThread()
获取当前线程
- 然后通过
getId()
和getName()
获取当前线程的id和名称
public class ThreadTest {
public static void main(String[] args) {
Thread currentThread = Thread.currentThread();
System.out.printf("Main, Which Thread: %s, %d%n",
currentThread.getName(),
currentThread.getId());
Abc abc = new Abc();
abc.start();
}
}
class Abc extends Thread {
@Override
public synchronized void start() {
super.start();
Thread currentThread = Thread.currentThread();
System.out.printf("Start, Which Thread: %s, %d%n",
currentThread.getName(),
currentThread.getId());
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread" + " " + i);
}
}
@Override
public void run() {
super.run();
Thread currentThread = Thread.currentThread();
System.out.printf("Run, Which Thread: %s, %d%n",
currentThread.getName(),
currentThread.getId());
}
}
对我来说这个returns:
Main, Which Thread: main, 1
Start, Which Thread: main, 1
Run, Which Thread: Thread-0, 9
Thread 0
Thread 1
Thread 2
Thread 3
....
这证明您的 Abc 的 start 方法(正如预期的那样)在与调用代码相同的线程中被调用,而不是在新线程中被调用,并且只有 run()
方法中的代码被调用一个新线程。如上所述,您几乎不需要或不想扩展 Thread,而是实现 Runnable 或 Callable。