通过创建 Thread 的子类,然后从主类创建、初始化和启动两个 Thread 对象来创建多线程程序。线程将在合适的接口中执行以下输出。
OUTPUT :
thread1: Java
thread1: is
thread2: Java
thread1: an
thread2: is
thread1: 令人兴奋
thread2: an
thread1: new
thread2: 令人兴奋
thread1: 语言
thread1: for
thread1: 并发
thread2: new
thread1: 编程。
thread2: 语言
thread2: for
thread2: 并发
thread2: 编程。
这是我的编码似乎先调用线程 1,然后调用线程 2。如何让它像输出一样显示。
class Thread1 extends Thread {
public void run() {
System.out.println("Thread1: Java");
System.out.println("Thread1: is ");
System.out.println("Thread1: exciting ");
System.out.println("Thread1: new ");
System.out.println("Thread1: language ");
System.out.println("Thread1: for ");
System.out.println("Thread1: concurrent ");
System.out.println("Thread1: programming ");
}
}
class Thread2 extends Thread {
public void run() {
System.out.println("Thread2 Java");
System.out.println("Thread2: an ");
System.out.println("Thread2: is ");
System.out.println("Thread2: an ");
System.out.println("Thread2: exciting");
System.out.println("Thread2: new");
System.out.println("Thread2: language");
System.out.println("Thread2: for");
System.out.println("Thread2: concurrent");
System.out.println("Thread2: programming");
suspend();
}
}
class Thread3 extends Thread
{
public void run()
{
System.out.print("Thread3");
try
{
sleep(1000);
}
catch(Exception e)
{
}
System.out.print(" Running");
}
}
class ThreadDemo3
{
public static void main(String args[]) throws InterruptedException
{
Thread1 obj1 = new Thread1();
obj1.start();
Thread2 obj2 = new Thread2();
obj2.start();
}
}