做下线程测试 感觉线程里的委托就是子另一个子线程, 我不知道我这样理解是对是错?求高手!!!
因为是刚刚接触线程 所以不怎么看的懂 ···
子线程就是委托参数方法吗
答案:2 悬赏:40 手机版
解决时间 2021-03-09 06:13
- 提问者网友:沉默菋噵
- 2021-03-08 23:53
最佳答案
- 五星知识达人网友:拜訪者
- 2021-03-09 00:25
构造一个具有返回值的线程委托。using System;
using System.Collections.Generic;
//using System.Linq;我用的是VS2010.
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyThread obj = new MyThread();
//设定线程函数参数
obj.x = 100;
obj.y = 200;
//创建线程对象
Thread th = new Thread(new ThreadStart(obj.ThreadMethod));
//启动线程
th.Start();
//主线程干其他事
th.Join(); //等待辅助线程结束
//取回线程结果
Console.Write(obj.returnValue);
}
}
class MyThread
{
public int x;
public int y;
public long returnValue;
public long SomeFunc(int x, int y)
{
long ret = 0;
//使用x和y完成一些工作
return ret;
}
public void ThreadMethod()
{
returnValue = SomeFunc(x, y);
}
}
}
using System.Collections.Generic;
//using System.Linq;我用的是VS2010.
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyThread obj = new MyThread();
//设定线程函数参数
obj.x = 100;
obj.y = 200;
//创建线程对象
Thread th = new Thread(new ThreadStart(obj.ThreadMethod));
//启动线程
th.Start();
//主线程干其他事
th.Join(); //等待辅助线程结束
//取回线程结果
Console.Write(obj.returnValue);
}
}
class MyThread
{
public int x;
public int y;
public long returnValue;
public long SomeFunc(int x, int y)
{
long ret = 0;
//使用x和y完成一些工作
return ret;
}
public void ThreadMethod()
{
returnValue = SomeFunc(x, y);
}
}
}
全部回答
- 1楼网友:青灯有味
- 2021-03-09 01:22
public class demo { public static demo1 d1; public static void main(string args[]) { new thread(new runnable() { public void run() { d1 = new demo1(); } }).start(); try { thread.currentthread().sleep(10); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.print(d1.getname()); }}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯