A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 风雪再现 中级黑马   /  2013-5-7 18:05  /  1389 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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);
        }
    }

我在学习多线程,但是,看视频的时候,没有看出来这部分在多线程中怎么用的,感觉好像没有用到,请帮助

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

6 个回复

倒序浏览
这个只有这一部分的代码也还真看不出这段是有什么用啊
回复 使用道具 举报
LZ这代码是从哪里来的啊(我没在基础课程的Java代码里看过咧..............),另外LZ贴的这段代码跟多线程的特性一点关系都没有,坑的我好惨,.................O(∩_∩)O~
(不说了,贴解释,O(∩_∩)O~,LZ被刺激了没?呵呵O(∩_∩)O~)

222本.JPG (181.26 KB, 下载次数: 0)

222本.JPG

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
期待高手出个多线程的帖子。。俺 也不会
回复 使用道具 举报
//创建辅助线程,并用它与主线程并行执行处理。增加功能使一个线程等待另一个线程,并正确地终止线程。
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 21:57
//创建辅助线程,并用它与主线程并行执行处理。增加功能使一个线程等待另一个线程,并正确地终止线程。
usi ...

英文注释伤不起啊~
回复 使用道具 举报
多线程实现由两种方式。一种是继承Thread()类,复写Thread()子类的run()方法。然后通过Thread()子类对象调用start()方法来启动线程。
第二种是通过实现Runnble()接口,复写Runnble()子类中的run()方法。最后创建Runnble()子类的对象。然后创建多个(要几个线程就创建几个)Thread()类的对象,将Runnble的子类对象传给Thread()类的构造函数。最后让Thread()对象调用start()方法的方式来实现多线程。
不过你的代码里面貌似没有run方法,也没有实现Runnble接口,没有继承Thread类啊。。请问你的疑点在哪呢?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马