C#多线程问题:我想让运行结果为10个A或B,但结果只有1个A或B,怎样修改呢?
usingSystem;usingSystem.Threading;publicdelegatevoidParameterizedThreadStart(objectob...
using System;
using System.Threading;
public delegate void ParameterizedThreadStart (object obj);
class ThreadTest
{
static object locker = new object();
static bool done;
static void Main()
{
for (int k = 0; k < 10; k++)
{
Thread t = new Thread(Go);
t.Start(true);
Go(false);
Console.ReadLine();
}
}
static void Go(object upperCase)
{
lock (locker)
{
if (!done)
{
done = true;
bool upper = (bool)upperCase;
Console.WriteLine(upper ? "A" : "B");
}
}
}
} 展开
using System.Threading;
public delegate void ParameterizedThreadStart (object obj);
class ThreadTest
{
static object locker = new object();
static bool done;
static void Main()
{
for (int k = 0; k < 10; k++)
{
Thread t = new Thread(Go);
t.Start(true);
Go(false);
Console.ReadLine();
}
}
static void Go(object upperCase)
{
lock (locker)
{
if (!done)
{
done = true;
bool upper = (bool)upperCase;
Console.WriteLine(upper ? "A" : "B");
}
}
}
} 展开
2个回答
展开全部
static object locker = new object();
static bool done;
static void Main()
{
for (int k = 0; k < 10; k++)
{
Thread t = new Thread(Go);
t.Start(false);
// Go(false); 这个去掉,这个是函数调用
}
Console.ReadLine();//把这句话写到这个地方
}
static void Go(object upperCase)
{
lock (locker)
{
// if (!done) 不知道你这是做什么,要去掉
{
done = true;
bool upper = (bool)upperCase;
Console.WriteLine(upper ? "A" : "B");
}
}
}
static bool done;
static void Main()
{
for (int k = 0; k < 10; k++)
{
Thread t = new Thread(Go);
t.Start(false);
// Go(false); 这个去掉,这个是函数调用
}
Console.ReadLine();//把这句话写到这个地方
}
static void Go(object upperCase)
{
lock (locker)
{
// if (!done) 不知道你这是做什么,要去掉
{
done = true;
bool upper = (bool)upperCase;
Console.WriteLine(upper ? "A" : "B");
}
}
}
追问
加入排它锁和done是为了线程互斥访问啊
追答
你用了全局静态变量done,在它第一次运行时他就把done修改成true了,只它的程序用时肯定done就为true了,它就不执行if中的语句了
展开全部
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void ParameterizedThreadStart(object obj);
private delegate void FlushClient(object upperCase);//代理
object locker = new object();
bool done;
private bool aborting = true;
private void button1_Click(object sender, EventArgs e)
{
aborting = false;
Thread t = new Thread(new ThreadStart(Run));
t.Start();
Console.ReadLine();
}
void Go(object upperCase)
{
lock (locker)
{
if (!done)
{
done = false ;
bool upper = (bool)upperCase;
this.textBox1.Text += upper ? "A" : "B";
}
}
}
void Run()
{
while (!aborting)
{
for (int i = 0; i < 10; i++)
{
if (i == 9)
{
aborting = true;
}
FlushClient fc = new FlushClient(Go);
this.BeginInvoke(fc, new object[] { false });//通过代理调用刷新方法
System.Threading.Thread.Sleep(1000);
}
}
}
}
{
public Form1()
{
InitializeComponent();
}
public delegate void ParameterizedThreadStart(object obj);
private delegate void FlushClient(object upperCase);//代理
object locker = new object();
bool done;
private bool aborting = true;
private void button1_Click(object sender, EventArgs e)
{
aborting = false;
Thread t = new Thread(new ThreadStart(Run));
t.Start();
Console.ReadLine();
}
void Go(object upperCase)
{
lock (locker)
{
if (!done)
{
done = false ;
bool upper = (bool)upperCase;
this.textBox1.Text += upper ? "A" : "B";
}
}
}
void Run()
{
while (!aborting)
{
for (int i = 0; i < 10; i++)
{
if (i == 9)
{
aborting = true;
}
FlushClient fc = new FlushClient(Go);
this.BeginInvoke(fc, new object[] { false });//通过代理调用刷新方法
System.Threading.Thread.Sleep(1000);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询