黑马程序员技术交流社区
标题: 多线程的问题 [打印本页]
作者: 风雪再现 时间: 2013-5-7 18:05
标题: 多线程的问题
public class MyThread
{
ThreadStart ts;
ParameterizedThreadStart pts;
public MyThread (ThreadStart ts)//要求用户在new的时候传入方法委托
{
this.ts=ts;//将委托赋给全局变量
}
public void Start()
{
ts();//调用委托,执行委托里的方法
}
public MyThread (ParameterizedThreadStart pts)
{
this.pts=pts;
}
public void Start(object o)
{
pts(o);
}
}
我在学习多线程,但是,看视频的时候,没有看出来这部分在多线程中怎么用的,感觉好像没有用到,请帮助
作者: 白磊 时间: 2013-5-7 19:10
这个只有这一部分的代码也还真看不出这段是有什么用啊
作者: zms2100 时间: 2013-5-7 19:13
LZ这代码是从哪里来的啊(我没在基础课程的Java代码里看过咧..............),另外LZ贴的这段代码跟多线程的特性一点关系都没有,坑的我好惨,.................O(∩_∩)O~
(不说了,贴解释,O(∩_∩)O~,LZ被刺激了没?呵呵O(∩_∩)O~)
-
222本.JPG
(181.26 KB, 下载次数: 0)
作者: net七期王雷 时间: 2013-5-7 20:46
期待高手出个多线程的帖子。。俺 也不会
作者: 许庭洲 时间: 2013-5-7 21:57
//创建辅助线程,并用它与主线程并行执行处理。增加功能使一个线程等待另一个线程,并正确地终止线程。
usingSystem;
usingSystem.Threading;
publicclass Worker
{
// This method will be called when thethread is started.
public void DoWork()
{
while (!_shouldStop)
{
Console.WriteLine("workerthread: working...");
}
Console.WriteLine("worker thread:terminating gracefully.");
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile is used as hint to the compilerthat this data
// member will be accessed by multiplethreads.
private volatile bool _shouldStop;
}
publicclass WorkerThreadExample
{
static void Main()
{
// Create the thread object. This doesnot start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start();
Console.WriteLine("main thread:Starting worker thread...");
// Loop until worker thread activates.
while (!workerThread.IsAlive);
// Put the main thread to sleep for 1millisecond to
// allow the worker thread to do somework:
Thread.Sleep(1);
// Request that the worker thread stopitself:
workerObject.RequestStop();
// Use the Join method to block the currentthread
// until the object's threadterminates.
workerThread.Join();
Console.WriteLine("main thread:Worker thread has terminated.");
}
}
输出如下:
mainthread: starting worker thread...
workerthread: working...
workerthread: working...
workerthread: working...
workerthread: working...
workerthread: working...
workerthread: working...
workerthread: working...
workerthread: working...
workerthread: working...
workerthread: working...
workerthread: working...
worker thread:terminating gracefully...
mainthread: worker thread has terminated
作者: 陈行 时间: 2013-5-7 22:26
许庭洲 发表于 2013-5-7 21:57
//创建辅助线程,并用它与主线程并行执行处理。增加功能使一个线程等待另一个线程,并正确地终止线程。
usi ...
英文注释伤不起啊~
作者: 黑马-雷钊 时间: 2013-5-10 20:42
多线程实现由两种方式。一种是继承Thread()类,复写Thread()子类的run()方法。然后通过Thread()子类对象调用start()方法来启动线程。
第二种是通过实现Runnble()接口,复写Runnble()子类中的run()方法。最后创建Runnble()子类的对象。然后创建多个(要几个线程就创建几个)Thread()类的对象,将Runnble的子类对象传给Thread()类的构造函数。最后让Thread()对象调用start()方法的方式来实现多线程。
不过你的代码里面貌似没有run方法,也没有实现Runnble接口,没有继承Thread类啊。。请问你的疑点在哪呢?
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |