public class Test extends Thread { private String sThreadName; public static void main(String argv[]) { Test h = new Test(); h.go(); } Test() { } Test(String s) { sThreadName = s; } public String getThreadName() { return sThreadName; } public void go() { Test first = new Test("first"); first.start(); Test second = new Test("second"); second.start(); } public void start() { for (int i = 0; i < 2; i++) { System.out.println(getThreadName() + i); try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } } }
时间: 2024-03-14 16:49:36 浏览: 12
这段代码定义了一个名为 Test 的类,继承自 Thread 类。在 main 方法中,创建了一个 Test 类的实例 h,并调用了它的 go 方法。在 go 方法中,又创建了两个 Test 类的实例 first 和 second,并分别调用它们的 start 方法,启动了两个新的线程。
在 Test 类中,重写了 start 方法,在其中使用了 for 循环打印当前线程的名称和一个递增的数字。每次打印完后,线程暂停 100 毫秒。由于创建了两个 Test 类的实例,因此总共会启动两个线程,每个线程都会打印两次输出。
需要注意的是,由于每个线程的 start 方法中都会调用 Test 类的 start 方法,因此实际上是在创建了一个新的线程后,再调用了这个新线程的 start 方法。因此,每个线程的名称都是在新线程中生成的,而不是在构造函数中指定的。在本例中,由于重写了 start 方法,并没有调用 Thread 类的 start 方法,因此线程不会并发执行,而是按顺序依次执行。因此,最终的输出结果是:
first0
first1
second0
second1
相关问题
<T extends Comparable<? super T>>
This is a generic type parameter declaration in Java. It specifies a type parameter T that is constrained to be a subtype of Comparable<? super T>. This means that T must be a type that can be compared to itself or to any superclass of T.
The syntax of this declaration is as follows:
- <T>: This declares a type parameter named T.
- extends Comparable<? super T>: This constrains T to be a subtype of Comparable, where the type parameter of Comparable is ? super T. This means that T must be comparable to itself or to any superclass of T.
Here is an example of how this type parameter might be used in a generic class definition:
```
public class MyClass<T extends Comparable<? super T>> {
// class definition goes here
}
```
In this example, MyClass is a generic class that takes a type parameter T that is constrained to be a subtype of Comparable<? super T>. This means that any instance of MyClass will have access to comparison methods like compareTo() that can be used to compare objects of type T.
public class Test extends Thread { private String sThreadName; public static void main(String[] args) { Test h = new Test(); h.go(); } Test() { } Test(String s) { sThreadName = s; } public String getThreadName() { return sThreadName; } public void go() { Test first = new Test("first"); first.start(); Test second = new Test("second"); second.start(); } public void start() { for (int i = 0; i < 2; i++) { System.out.println(getThreadName() + i); try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e.getMessage());
这段代码是一个继承自Thread类的多线程程序。在main方法中,创建了一个Test类的对象h,并调用了它的go()方法。在go()方法中,创建了两个Test类的对象first和second,并分别启动了它们的线程。在Test类的start()方法中,使用一个for循环来输出线程名和一个计数器i的值,并使用Thread.sleep()方法使线程休眠100毫秒。由于Test类继承自Thread类,因此它可以被启动为一个单独的线程,并且可以使用start()方法来启动它的线程。在本例中,由于Test类没有重写run()方法,因此默认使用Thread类的run()方法来执行线程。在run()方法中,会执行在start()方法中定义的for循环,因此会输出线程名和计数器的值。由于启动了两个Test类的对象,因此会有两个线程同时执行,并且会交替输出它们的线程名和计数器的值。