| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1660 人关注过本帖
标题:[求助]Thread运行顺序的问题。
只看楼主 加入收藏
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
 问题点数:0 回复次数:10 
[求助]Thread运行顺序的问题。

using System;
using System.Threading;

public class ServerClass
{
// The method that will be called when the thread is started.
public void InstanceMethod()
{
Console.WriteLine(
"ServerClass.InstanceMethod is running on another thread.");

// Pause for a moment to provide a delay to make
// threads more apparent.
Thread.Sleep(3000);
Console.WriteLine(
"The instance method called by the worker thread has ended.");
}

public static void StaticMethod()
{
Console.WriteLine(
"ServerClass.StaticMethod is running on another thread.");

// Pause for a moment to provide a delay to make
// threads more apparent.
Thread.Sleep(5000);
Console.WriteLine(
"The static method called by the worker thread has ended.");
}
}

public class Simple
{
public static int Main(String[] args)
{
Console.WriteLine("Thread Simple Sample");

ServerClass serverObject = new ServerClass();

// Create the thread object, passing in the
// serverObject.InstanceMethod method using a
// ThreadStart delegate.
Thread InstanceCaller = new Thread(
new ThreadStart(serverObject.InstanceMethod));

// Start the thread.
InstanceCaller.Start();

Console.WriteLine("The Main() thread calls this after "
+ "starting the new InstanceCaller thread.");

// Create the thread object, passing in the
// serverObject.StaticMethod method using a
// ThreadStart delegate.
Thread StaticCaller = new Thread(
new ThreadStart(ServerClass.StaticMethod));

// Start the thread.
StaticCaller.Start();

Console.WriteLine("The Main() thread calls this after "
+ "starting the new StaticCaller thread.");

return 0;
}
}

搜索更多相关主题的帖子: Thread 顺序 运行 
2006-08-28 17:19
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
Thread Simple Sample
The Main() thread calls this after starting the new InstanceCaller thread.
ServerClass.InstanceMethod is running on another thread.
The Main() thread calls this after starting the new StaticCaller thread.
ServerClass.StaticMethod is running on another thread.
线程 0x1eb8 已退出,返回值为 0 (0x0)。
线程 '<无名称>' (0x1b90) 已退出,返回值为 0 (0x0)。
The instance method called by the worker thread has ended.
线程 '<无名称>' (0x1da8) 已退出,返回值为 0 (0x0)。
The static method called by the worker thread has ended.
线程 '<无名称>' (0x1d80) 已退出,返回值为 0 (0x0)。
程序“[5308] msg.vshost.exe: 托管”已退出,返回值为 0 (0x0)。

这个是运行结果。


2006-08-28 17:20
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 

InstanceCaller.Start();

Console.WriteLine("The Main() thread calls this after "
+ "starting the new InstanceCaller thread.");

我的问题是:InstanceCaller.Start();以后,什么时候执行InstanceMethod()的方法?
是随机还是什么?

我以前理解的是要执行主进程Console.WriteLine("The Main() thread calls this after "
+ "starting the new InstanceCaller thread.");

Console.WriteLine("The Main() thread calls this after "
+ "starting the new StaticCaller thread.");

等这些全部执行完毕以后,才能够执行线程 。或者说在执行主进程的代码的时候,调用 thread.sleep()或者 InstanceCaller。join()才可以。

但是这个程序运行让我不明白。



2006-08-28 17:23
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 

結果不是預料中的?


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-28 17:28
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 

線程只是瓜分了執行片段而已
並不會主線程執行完畢,才其他線程

宏關上看是併發執行的

當InstanceCaller.Start();以後
那個微小的執行片段已經足夠執行
Console.WriteLine(
"ServerClass.InstanceMethod is running on another thread.");了

加如這裡執行的時間超過那個執行片段,當然就會按你所想的
The Main() thread calls this after starting the new InstanceCaller thread
這一句先出來


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-28 17:38
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
那你的意思是InstanceCaller。start()开的线程跟主进程的优先级是一样的,按照语句运行的顺序,先运行InstanceCaller。start()里的委托方法?一定时间以后,无论完成与否,再执行主线程console。writeline()?

2006-08-28 17:49
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 

對啊

优先级好像不會影響線程執行片段的時間吧
覺得只是影響順序


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-28 18:02
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
如果
public void InstanceMethod()
{
for (int j = 0; j < 10000000; j++) ; 新加的
Console.WriteLine("ServerClass.InstanceMethod is running on another thread.");
// Pause for a moment to provide a delay to make
// threads more apparent.
Thread.Sleep(3000);
Console.WriteLine(
"The instance method called by the worker thread has ended.");
}

执行结果:
Thread Simple Sample
ServerClass.InstanceMethod is running on another thread.这个为什么会提前执行呢?
The Main() thread calls this after starting the new InstanceCaller thread.
The Main() thread calls this after starting the new StaticCaller thread.
ServerClass.StaticMethod is running on another thread.

2006-08-28 18:09
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
我的本意是如果系统质分配给InstanceCaller。start()一定时间的话,那么用一个空循环消耗其时间,使程序在还没有运行到InstanceCaller里面委托的方法的输出语句的时候就已经消耗掉系统分配给他运行的时间了,这样一来,就应该先执行consle.writeline()
结果恰恰相反,这是为什么?

2006-08-28 18:12
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
很簡單 你機子運算速度太快了而已
再多加幾個0

你也可以試著把
for (int j = 0; j < 10000000; j++) ;
放到
InstanceCaller.Start();
後面

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-28 18:16
快速回复:[求助]Thread运行顺序的问题。
数据加载中...
 
   



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

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