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

// 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
J华
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-7-2
收藏
得分:0 

Main thread starting.
.Child #1 starting.
.... In Child #1,count is 0
..... In Child #1,count is 1
..... In Child #1,count is 2
..... In Child #1,count is 3
..... In Child #1,count is 4
..... In Child #1,count is 5
..... In Child #1,count is 6
..... In Child #1,count is 7
..... In Child #1,count is 8
..... In Child #1,count is 9
Child #1 terminating.
Main thread ending.

我也正开始看线程, 看结果就知道了啊..... 并不是start()就开始新线程,因为主线程和Child #1默认优先级都是5 但入口是主线程. 如果想一start()就开始其他线程 先设置他的优先级比主线程高.sleep()转到阻塞状态.

2007-08-26 00:16
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
J华
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-7-2
收藏
得分:0 
Thread.currentThread().getName()
2007-08-26 13:28
狂放不羁
Rank: 4
等 级:贵宾
威 望:12
帖 子:925
专家分:0
注 册:2007-1-24
收藏
得分:0 
学完操作系统的话,学线程就容易多了。这东西关键是要理解。。
2007-08-26 22:17
快速回复:多线程问题请教
数据加载中...
 
   



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

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