C#多线程实现打印出顺序ABC

anan221 2010-03-02 04:36:43
C#多线程实现打印出顺序ABC
...全文
423 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
anan221 2010-03-04
  • 打赏
  • 举报
回复
难道没有人知道吗??问题还没有解决
anan221 2010-03-04
  • 打赏
  • 举报
回复
继续顶,学习线程。。。。。。
harvesthouhao 2010-03-03
  • 打赏
  • 举报
回复
用lock和unlock不行吗?
夺命胖子 2010-03-03
  • 打赏
  • 举报
回复
不懂什么意思。。。

一个线程打印A 第二个线程打印B ,第三个线程打印C

三个线程同时操作一个变量

第二个线程等待第一个线程录入A后才能录入B,第三个线程等待第二个线程录入B后才能录入A?

难道是这样。。。
anan221 2010-03-03
  • 打赏
  • 举报
回复
没有人知道吗,继续顶,自己顶
anan221 2010-03-03
  • 打赏
  • 举报
回复
楼上是JAVA的处理的机制都不一样的
zzxap 2010-03-03
  • 打赏
  • 举报
回复
三个线程,其一只打印A,其一只打印B,其一只打印C,要求开启三线程顺序打印多次ABC。
对wait和notify(All)刚刚理解,不能错过这个练习的机会,然后再写个生产者-消费者的程序多练练。写的不好的、需要改进的地方还请JE上的朋友给建议^..^。
public class ThreadABC implements Runnable {
public static final int RUN_TOTAL_COUNT=30; //打印ABC的次数
public static volatile boolean arun=true; //开始打印A
public static volatile boolean brun=false;
public static volatile boolean crun=false;
public static final byte[] LOCK=new byte[0]; //互斥器
public void run() {
new Thread(new PrintB()).start();
new Thread(new PrintC()).start();
new Thread(new PrintA()).start();
}
private static class PrintA implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(arun){
LOCK.notifyAll();
System.out.print("A");
runCount++;
arun=false;
brun=true;
crun=false;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintA InterruptedException occured...");
}
}
} //end syn
}
}
private static class PrintB implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(brun){
LOCK.notifyAll();
System.out.print("B");
runCount++;
arun=false;
brun=false;
crun=true;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintB InterruptedException occured...");
}
}
} //end syn
}
}
private static class PrintC implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(crun){
LOCK.notifyAll();
System.out.print("C-");
runCount++;
arun=true;
brun=false;
crun=false;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintC InterruptedException occured...");
}
}
} //end syn
}
}

//test in main
public static void main(String[] args){
new Thread(new ThreadABC()).start();
}
}
anan221 2010-03-03
  • 打赏
  • 举报
回复
我贴出上面的代码,但有时候启动程序的时候,顺序就会搞错,不知道是什么问题
anan221 2010-03-03
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TestCat
{
class testPrintABC
{
#region variable
static Thread thread1 = null;
static Thread thread2 = null;
static Thread thread3 = null;
static Mutex mutex = null;
#endregion

static void Main(string[] args)
{
testPrintABC p = new testPrintABC();
p.RunThread();
Console.ReadLine();
}

public testPrintABC()
{
mutex = new Mutex();
thread1 = new Thread(new ThreadStart(thread1Func));
thread2 = new Thread(new ThreadStart(thread2Func));
thread3 = new Thread(new ThreadStart(thread3Func));
}



public void RunThread()
{
thread1.Start();
thread2.Start();
thread3.Start();
}

private void thread1Func()
{
for (int count = 0; count < 10; count++)
{
lock (this)
{
mutex.WaitOne();
TestFunc("Thread1 have run " + count.ToString() + " times:a");
Thread.Sleep(30);
mutex.ReleaseMutex();
}
}
}
private void thread2Func()
{
for (int count = 0; count < 10; count++)
{
lock (this)
{
mutex.WaitOne();
TestFunc("Thread2 have run " + count.ToString() + " times:b");
Thread.Sleep(100);
mutex.ReleaseMutex();
}
}
}

private void thread3Func()
{
for (int count = 0; count < 10; count++)
{
lock (this)
{
mutex.WaitOne();
TestFunc("Thread3 have run " + count.ToString() + " times:c");
Thread.Sleep(200);
mutex.ReleaseMutex();
}
}
}

private void TestFunc(string str)
{
//Monitor.Enter(this);

Console.WriteLine(str);
Thread.Sleep(50);
//Monitor.Exit(this);
}

}
}
anan221 2010-03-03
  • 打赏
  • 举报
回复
对,是根据四楼的意思
anan221 2010-03-02
  • 打赏
  • 举报
回复
老兄怎么用栈呢,C#用栈还没有用过
whb147 2010-03-02
  • 打赏
  • 举报
回复
用一个栈?
先进先出?

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧