我是Java的初学者,遇到过多线程。
0。我对用法“单个线程中的多个可运行接口” 和“单个线程中的多个线程”的使用感到困惑。 / em>。请问外行人有什么区别?
1。我是说以下代码是“单个可运行接口中有多个线程”吗??
这就是我所做的:
class NewThread implements Runnable{
Thread t1, t2, t3;
NewThread(){
t1 = new Thread(this, "Demo Thread 1");
t2 = new Thread(this, "Demo Thread 2");
t3 = new Thread(this, "Demo Thread 3");
t1.start(); //it automatically invokes run() method
t2.start();
t3.start();
}
public void run(){
try{
System.out.println("Child Thread 1: "+t1);
t1.sleep(1000);
System.out.println("Child Thread 2: "+t2);
t2.sleep(1000);
System.out.println("Child Thread 3: "+t3);
t3.sleep(1000);
}
catch(InterruptedException e){
System.out.println("Child Interrupted.");
}
System.out.println("Exiting child thread.");
}
}
public class Main{
public static void main(String[] args) {
NewThread NT = new NewThread();
}
}
这是输出:
Child Thread 1: Thread[Demo Thread 1,5,main]
Child Thread 1: Thread[Demo Thread 1,5,main]
Child Thread 1: Thread[Demo Thread 1,5,main]
Child Thread 2: Thread[Demo Thread 2,5,main]
Child Thread 2: Thread[Demo Thread 2,5,main]
Child Thread 2: Thread[Demo Thread 2,5,main]
Child Thread 3: Thread[Demo Thread 3,5,main]
Child Thread 3: Thread[Demo Thread 3,5,main]
Child Thread 3: Thread[Demo Thread 3,5,main]
Exiting child thread.
Exiting child thread.
Exiting child thread.
2。。为什么输出像一个循环,我的意思是:
Child Thread 1 ...
Child Thread 1 ...
Child Thread 1 ...
Child Thread 2 ...
Child Thread 2 ...
Child Thread 2 ...
如果我注释掉t2
,那么输出将变为:
Child Thread 1 ...
Child Thread 1 ...
Child Thread 3 ...
Child Thread 3 ...
?
更新1
我预期的输出为:
Child Thread 1 Thread[Demo Thread 1,5,main]
Child Thread 2 Thread[Demo Thread 2,5,main]
Child Thread 3 Thread[Demo Thread 3,5,main]
Exiting child thread.
Exiting child thread.
Exiting child thread.
3。。所有这些线程是否都在JVM中同时执行?
4。。5
中的main
和... 5,main]
被打印为线程对象是什么?
更新2
我将try
部分代码更新为:
System.out.println(Thread.currentThread().getName());
System.out.println("Child Thread 1: "+t1);
t1.sleep(1000);
System.out.println(Thread.currentThread().getName());
System.out.println("Child Thread 2: "+t2);
t2.sleep(1000);
System.out.println(Thread.currentThread().getName());
System.out.println("Child Thread 3: "+t3);
t3.sleep(1000);
现在输出让我更加困惑:
Demo Thread 2
Demo Thread 3
Demo Thread 1
Child Thread 1: Thread[Demo Thread 1,5,main]
Child Thread 1: Thread[Demo Thread 1,5,main]
Child Thread 1: Thread[Demo Thread 1,5,main]
Demo Thread 1
Demo Thread 2
Demo Thread 3
Child Thread 2: Thread[Demo Thread 2,5,main]
Child Thread 2: Thread[Demo Thread 2,5,main]
Child Thread 2: Thread[Demo Thread 2,5,main]
Demo Thread 2
Demo Thread 1
Child Thread 3: Thread[Demo Thread 3,5,main]
Demo Thread 3
Child Thread 3: Thread[Demo Thread 3,5,main]
Child Thread 3: Thread[Demo Thread 3,5,main]
Exiting child thread.
Exiting child thread.
Exiting child thread.
5。顺序也被更改(在不同的运行中)...到底发生了什么?
6。。由于我在初始化期间将this
用于t1 t2 & t3
,它们基本上是同一线程吗?如果没有,那是什么意思?
7。。如何故意中断任何一个线程,以便测试引发的异常?
请原谅我的好奇心。
谢谢
答案 0 :(得分:-1)
我认为“接口线程”的句子也很奇怪。是否有线程,它使用Runnable接口定义在其中执行的操作。调用t.start()后,您将拥有一个新线程。
2:输出看起来像一个循环,因为所有3个线程都执行第一个打印,然后执行第二个打印,然后执行该线程。查看当前线程的更好方法是打印command.CommandText = "insert into MasbrgSpring (KODEC, WIRE, DIMENSION, STD, NOMLOT, STAT) VALUES (@kodec, @wire,@dimension,@std,@nomlot,@stat)"
3:是
4:Thread.toString()打印线程的名称,优先级和线程组。因此,“ main”是初始主线程以及该线程组的名称。
-我认为您的误解是因为您有一个名为NewThread的类,但这实际上是一个Runnable,而不是线程。尝试改为理解此示例:
Thread.currentThread().getName()