public class Thread_Demo
{
public static void main(String[] args)
{
FirstThread li = new FirstThread("li"); //请问这个有什么作用????
FirstThread wu = new FirstThread("wu");
FirstThread hu = new FirstThread("hu");
Thread thread1 = new Thread(li); //请问这个又有什么作用????
Thread thread2 = new Thread(wu);
Thread thread3 = new Thread(hu);
second_thread thread4 = new second_thread(4);//为什么用继承thread的方式,就不用像上面一样.
thread2.start();
thread3.start();
thread1.start();
thread4.start();
}
}
class FirstThread implements Runnable
{
private String name;
public FirstThread(String name)
{
this.name = name;
}
public void run()
{
while (true)
{
System.out.println("name=" + name);
System.out.println("-----------");
Thread.yield();
}
}
}
class second_thread extends Thread
{
private int num;
public second_thread(int num)
{
this.num = num;
}
public void run()
{
while (true)
{
System.out.println("num=" + num);
System.out.print("**********");
Thread.yield();
}
}
}
----------------解决方案--------------------------------------------------------
这是java语言中,使用线程的两种方式:一种是继承Thread类,一种是实现Runnable接口。
second_thread thread4 = new second_thread(4);//用Thread()构造函数直接实例化一个线 程。 thread4.start() //启动线程。
FirstThread li = new FirstThread("li"); //实现Runnable接口
Thread thread1 = new Thread(li); //使用Thread(Runnable target)构造函数实例化一个线 程。
thread1.start() ; //启动线程。
不要太在意形式,他们实现的功能都一样。这是Java实现线程的两种机制。
----------------解决方案--------------------------------------------------------
谢谢,changyawei,只是想把它搞清楚点.
----------------解决方案--------------------------------------------------------
2楼经典~
----------------解决方案--------------------------------------------------------