| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 511 人关注过本帖
标题:多线程问题请教
取消只看楼主 加入收藏
lzyren2008
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2007-8-12
收藏
 问题点数:0 回复次数:1 
多线程问题请教

// 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(),请各位兄台指点一二,非常感谢!

搜索更多相关主题的帖子: 线程 
2007-08-25 11:34
lzyren2008
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2007-8-12
收藏
得分:0 

// 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(1000);
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 t=Thread.currentThread(); >>display currentThread
System.out.println(t);
Thread.sleep(500);
}
catch(InterruptedException exc)
{
System.out.println("Main thread interrupted.");
}
}while(mt.count!=10);

System.out.println("Main thread ending.");

}
现在执行顺序弄清楚了,谢谢兄台指点。如上在>>处清添加的显示当前线程的语句,为何不起作用,无法显示当前线程。

2007-08-26 10:10
快速回复:多线程问题请教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034569 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved