C# 使用C使用多线程写入文本文件#
希望你能在这方面帮助我。我是C多线程编程的初学者 我正在尝试构建一个程序,使用两个线程将范围从1到2000的所有数字写入两个文本文件 每个线程都应该写入在两个文件中任何一个都找不到的从1到2000的数字“文件中没有重复的数字”,并且每个线程都不应该写入由另一个线程写入的数字 最后,如果我们合并了两个文件的编号,那么编号应该是1到2000 这是我正在尝试的源代码,但在下图中编写for循环时出现了一个问题 我无法处理两个同步线程的写入过程,我有: 从未同步的代码块调用了对象同步方法C# 使用C使用多线程写入文本文件#,c#,multithreading,file,C#,Multithreading,File,希望你能在这方面帮助我。我是C多线程编程的初学者 我正在尝试构建一个程序,使用两个线程将范围从1到2000的所有数字写入两个文本文件 每个线程都应该写入在两个文件中任何一个都找不到的从1到2000的数字“文件中没有重复的数字”,并且每个线程都不应该写入由另一个线程写入的数字 最后,如果我们合并了两个文件的编号,那么编号应该是1到2000 这是我正在尝试的源代码,但在下图中编写for循环时出现了一个问题 我无法处理两个同步线程的写入过程,我有: 从未同步的代码块调用了对象同步方法 使用系统; 使用
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用系统线程;
命名空间多线程
{
班级计划
{
静态TextWriter file2=新的StreamWriter(“file2位置”);
静态TextWriter file1=新的StreamWriter(“file1位置”);
静态void Main(字符串[]参数)
{
尝试
{
int[]数组=新int[2000];
Thread thread1=新线程(Program.writeinfile1);
Thread thread2=新线程(Program.writeinfile2);
对于(int counter=1;counter,这里有一个多线程调用相互对话以确保它们不会重复工作的示例。
我没有完全按照你的要求去做,因为这看起来很像家庭作业;但希望这能帮助你找到解决问题的方法
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
new Program();
Console.WriteLine("done");
Console.ReadKey();
}
Program()
{
int noThreads = 5;
int target = 2000;
StartThread(noThreads, target);
}
//kicks off our threads / waits for all threads to complete before returning
void StartThread(int noThreads, int target)
{
int id = noThreads--;
if (id > 0)
{
Doer doer = new Doer(id, target);
Thread t = new Thread(doer.Do);
t.Start();
StartThread(noThreads,target);
t.Join();
}
}
}
class Doer
{
static int marker = 0;
static readonly object syncLocker = new object();
readonly int id;
readonly int target;
public Doer(int id, int target)
{
this.id = id;
this.target = target;
}
public void Do()
{
while (marker < this.target)
{
int i;
lock (syncLocker)
{
i = ++marker;
}
System.Console.WriteLine("{0:00}: {1:###0}", id, i);
//Thread.Sleep(RandomNo()); //uncomment this & code below if your threads are taking turns / behaving too predictably
}
}
/*
static readonly Random rnd = new Random();
static readonly object rndSyncLocker = new object();
public static int RandomNo()
{
lock (rndSyncLocker)
{
return rnd.Next(0, 1000);
}
}
*/
}
}
使用系统;
使用System.Collections.Generic;
使用系统线程;
使用System.Threading.Tasks;
命名空间堆栈溢出
{
班级计划
{
静态void Main(字符串[]参数)
{
新程序();
控制台。写入线(“完成”);
Console.ReadKey();
}
程序()
{
int=5;
int目标=2000;
StartThread(noThreads,target);
}
//启动线程/等待所有线程完成后再返回
void StartThread(int-noThreads,int-target)
{
int id=noThreads--;
如果(id>0)
{
实干者=新实干者(id,目标);
线程t=新线程(doer.Do);
t、 Start();
StartThread(noThreads,target);
t、 Join();
}
}
}
阶级实践者
{
静态int标记=0;
静态只读对象syncLocker=新对象();
只读int-id;
只读int目标;
公共行为人(内部id,内部目标)
{
this.id=id;
this.target=目标;
}
公营部门
{
while(标记
您没有正确使用监视器
类。应该调用对监视器.pulsell(thread2);
的调用,在这种情况下,该调用将在writeinfile1
和writeinfile2
方法中
这就是为什么会出现异常:
从未同步的代码块调用了对象同步方法
有关使用监视器.pulsell(对象)
的正确方法,请参阅以下堆栈溢出问题:
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
new Program();
Console.WriteLine("done");
Console.ReadKey();
}
Program()
{
int noThreads = 5;
int target = 2000;
StartThread(noThreads, target);
}
//kicks off our threads / waits for all threads to complete before returning
void StartThread(int noThreads, int target)
{
int id = noThreads--;
if (id > 0)
{
Doer doer = new Doer(id, target);
Thread t = new Thread(doer.Do);
t.Start();
StartThread(noThreads,target);
t.Join();
}
}
}
class Doer
{
static int marker = 0;
static readonly object syncLocker = new object();
readonly int id;
readonly int target;
public Doer(int id, int target)
{
this.id = id;
this.target = target;
}
public void Do()
{
while (marker < this.target)
{
int i;
lock (syncLocker)
{
i = ++marker;
}
System.Console.WriteLine("{0:00}: {1:###0}", id, i);
//Thread.Sleep(RandomNo()); //uncomment this & code below if your threads are taking turns / behaving too predictably
}
}
/*
static readonly Random rnd = new Random();
static readonly object rndSyncLocker = new object();
public static int RandomNo()
{
lock (rndSyncLocker)
{
return rnd.Next(0, 1000);
}
}
*/
}
}