// Create a thread by implementing Runnable.
class MyThread implements Runnable {
int count;
String thrdName;
MyThread(String name) {
count=0;
thrdName=name;
}
//Entry point of thread.
public void run() {
System.out.println(thrdName+" starting.");
try {
do {
Thread.sleep(500);
System.out.println(" In "+thrdName+
",count is "+count);
count++;
}while(count<10);
}
catch(InterruptedException exc) {
System.out.println(thrdName+" interrupted.");
}
System.out.println(thrdName+" terminating.");
}
}
class UseThread {
public static void main(String args[]) {
System.out.println("Main thread starting.");
// First,construct a MyThread object.
MyThread mt=new MyThread("Child #1");
// Next,construct a thread rom that object.
Thread newThrd=new Thread(mt);
//Finally,start execution of the thread.
newThrd.start();
do{
System.out.print(".");
try {
Thread.sleep(100);
}
catch(InterruptedException exc)
{
System.out.println("Main thread interrupted.");
}
}while(mt.count!=10);
System.out.println("Main thread ending.");
}
}
现在学习多线程原理,如上这个程序,请问它是如何运行的,运行步骤是怎样的?在调用start()函数时是否已开始运行新线程,运行到什么时候返回main(),请各位兄台指点一二,非常感谢!