多线程的顺序执行
有4个类,分别开一个线程,怎样才能按照a-b-c-d的顺序执行呢? 最好能贴出代码窗体代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
A a = new A();
B b = new B();
C c = new C();
D d = new D();
Thread t1=new Thread (new ThreadStart (a.a));
Thread t2 = new Thread(new ThreadStart(b.b));
Thread t3 = new Thread(new ThreadStart(c.c));
Thread t4 = new Thread(new ThreadStart(d.d));
t1.Start();
t2.Start();
t3.Start();
t4.Start();
}
类代码:
class A
{
public void a()
{
//方法
}
}
class B
{
public void b()
{
//方法
}
}
class C
{
public void c()
{
//方法
}
}
class D
{
public void d()
{
//方法
}
}
怎样才能按照a-b-c-d的顺序执行,最好能贴出代码 --------------------编程问答-------------------- 咋没人回复呢? --------------------编程问答-------------------- Join --------------------编程问答-------------------- 如果你是说执行完A以后,在执行B可以yon下面的方法
--------------------编程问答-------------------- 如果说是A开始以后接着执行B,则这样:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace test
{
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
C c = new C();
D d = new D();
Thread t1 = new Thread(new ThreadStart(a.a));
Thread t2 = new Thread(new ThreadStart(b.b));
Thread t3 = new Thread(new ThreadStart(c.c));
Thread t4 = new Thread(new ThreadStart(d.d));
t1.Start();
t1.Join();
t2.Start();
t2.Join();
t3.Start();
t3.Join();
t4.Start();
t4.Join();
Console.Read();
}
}
class A
{
public void a()
{
//方法
Console.WriteLine("Execute a().\n");
}
}
class B
{
public void b()
{
//方法
Console.WriteLine("Execute b().\n");
}
}
class C
{
public void c()
{
//方法
Console.WriteLine("Execute c().\n");
}
}
class D
{
public void d()
{
//方法
Console.WriteLine("Execute d().\n");
}
}
}
using System;--------------------编程问答--------------------
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace test
{
class StataInfo
{
public ManualResetEvent preEnent;
public ManualResetEvent curEnent;
public StataInfo(ManualResetEvent preEnent, ManualResetEvent curEnent)
{
this.preEnent = preEnent;
this.curEnent = curEnent;
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
C c = new C();
D d = new D();
Thread t1 = new Thread(new ParameterizedThreadStart(a.a));
Thread t2 = new Thread(new ParameterizedThreadStart(b.b));
Thread t3 = new Thread(new ParameterizedThreadStart(c.c));
Thread t4 = new Thread(new ParameterizedThreadStart(d.d));
StataInfo stataA = new StataInfo(null, new ManualResetEvent(false));
t1.Start(stataA);
StataInfo stataB = new StataInfo(stataA.curEnent, new ManualResetEvent(false));
t2.Start(stataB);
StataInfo stataC = new StataInfo(stataB.curEnent, new ManualResetEvent(false));
t3.Start(stataC);
StataInfo stataD = new StataInfo(stataC.curEnent, null);
t4.Start(stataD);
Console.Read();
}
}
class A
{
public void a(object stata)
{
StataInfo stataInfo = (StataInfo)stata;
if (stataInfo.preEnent != null)
stataInfo.preEnent.WaitOne();
if (stataInfo.curEnent != null)
stataInfo.curEnent.Set();
//方法
Console.WriteLine("Execute a().\n");
}
}
class B
{
public void b(object stata)
{
StataInfo stataInfo = (StataInfo)stata;
if (stataInfo.preEnent != null)
stataInfo.preEnent.WaitOne();
if (stataInfo.curEnent != null)
stataInfo.curEnent.Set();
//方法
Console.WriteLine("Execute b().\n");
}
}
class C
{
public void c(object stata)
{
StataInfo stataInfo = (StataInfo)stata;
if (stataInfo.preEnent != null)
stataInfo.preEnent.WaitOne();
if (stataInfo.curEnent != null)
stataInfo.curEnent.Set();
//方法
Console.WriteLine("Execute c().\n");
}
}
class D
{
public void d(object stata)
{
StataInfo stataInfo = (StataInfo)stata;
if (stataInfo.preEnent != null)
stataInfo.preEnent.WaitOne();
if (stataInfo.curEnent != null)
stataInfo.curEnent.Set();
//方法
Console.WriteLine("Execute d().\n");
}
}
}
static void Main(string[] args)
{
A a = new A();
B b = new B();
C c = new C();
D d = new D();
Thread t1 = new Thread(new ThreadStart(a.a));
Thread t2 = new Thread(new ThreadStart(b.b));
Thread t3 = new Thread(new ThreadStart(c.c));
Thread t4 = new Thread(new ThreadStart(d.d));
t1.Start();
t1.Join(1000); //在继续执行主线程前等待t1线程执行完毕,并指定1秒超时时长
t2.Start();
t2.Join(1000);
t3.Start();
t3.Join(1000);
t4.Start();
Console.ReadKey();
}
楼上都说了 --------------------编程问答-------------------- 晕 提交慢了... --------------------编程问答-------------------- 设置一个全局变量 --------------------编程问答-------------------- 你可以考虑把他们设置为不同的优先级别 --------------------编程问答-------------------- 是执行完A后执行B,执行完B后执行C,执行完C后执行D
用join()方法,主线程也堵塞了,和不用线程有什么区别? --------------------编程问答-------------------- 我有个笨办法,在A方法里结束的地方执t2.start()
在B的结束处执行t3.start()
类推~~ --------------------编程问答-------------------- 我的第二种方法可以阿,只要A开始执行了,就可以执行B了.. --------------------编程问答-------------------- 如果前一个线程因为某种原因挂起或阻塞怎么办??我认为这里的方法除了我的以外其他的都行不通 --------------------编程问答-------------------- 奇怪
为什么不用一个方法呢??
public void E
{
a();
b();
c();
d();
}
用一个线程就可以了啊。。。 --------------------编程问答-------------------- 可能他的几个方法是有可能无法执行完的吧.... --------------------编程问答-------------------- to:newnazi
线程一开始运行怎么会挂起?优先级别高的就一定会先执行吗?
楼主的意思只是按顺序执行,不过到底是怎么按顺序执行没有说清楚。 --------------------编程问答-------------------- LS:
t1.Start 是什么意思? 你能理解吗?
如果这个线程无法得到时间片当然会被挂起 既是Start方法你确定证哪个线程先被执行吗?
如果用一个线程到可以保证前面的代码先被执行. --------------------编程问答-------------------- 我觉得这个问题本身就很有问题.
既然不需要异步,又要顺序,何需要多线程??
a();
b();
c();
d();
不是最简单最好么? --------------------编程问答-------------------- 自用C#以来,很少用到涉及多线程的东西,最多的是System.Threading.Thread.Sleep(int x); --------------------编程问答-------------------- 路过.................. --------------------编程问答--------------------
++
补充:.NET技术 , C#