如何在C#中创建线程以及如何调用线程 [英] how to create thread in c# and how to call thread
本文介绍了如何在C#中创建线程以及如何调用线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此master.cs文件.
this master.cs file.
public class Master
{
internal BufferedReader br;
internal long volume;
internal int velocity;
internal string data; //variety
internal long mmemory; //master memory
internal int mLowEnd;
internal int mHighEnd;
internal long mCpuCap; // master cpu memory
internal int mcLowEnd;
internal int mcHighEnd;
internal int noOfSlaves; //slaves
internal long smemorycap; //slave memory
internal int sLowEnd;
internal int sHighEnd;
internal int sRandom;
internal int sClustered;
internal long sCpuCap; // slave cpu memory
internal int scLowEnd;
internal int scHighEnd;
internal int scRandom;
internal int scClustered;
internal long startTime;
internal long endTime;
internal Master()
{
try
{
br = new BufferedReader(new InputStreamReader(System.in));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public virtual void readInput()
{
try
{
Console.WriteLine("Volume (enter the volume of the bigdata in TB):");
volume = Convert.ToInt64(br.readLine());
Console.WriteLine("Velocity (enter the velocity of the bigdata in bps):");
velocity = Convert.ToInt32(br.readLine());
Console.WriteLine("Variety (enter the mix of the bigdata, the format to be determined by you):");
data = br.readLine();
Console.WriteLine("Master node memory (enter the memory capacity range of the master node in increments of 10GB)");
mmemory = Convert.ToInt64(br.readLine());
Console.WriteLine("Enter the low end in GB:");
mLowEnd = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the high end in GB:");
mHighEnd = Convert.ToInt32(br.readLine());
Console.WriteLine("Master node CPU (enter the CPU capacity range of the master node in increments of 10GHz)");
mCpuCap = Convert.ToInt64(br.readLine());
Console.WriteLine("Enter the low end in GHz:");
mcHighEnd = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the high end in GHz:");
mcLowEnd = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the number of slave nodes (1 - n):");
noOfSlaves = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the connectivity (enter 1 to n for 1:1 to 1:n connectivity, respectively):");
Console.WriteLine("Enter the memory capacity range of the slave nodes in increments of 10GB Enter the low end in GB:");
smemorycap = Convert.ToInt64(br.readLine());
Console.WriteLine("Enter the low end in GB:");
sLowEnd = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the high end in GB:");
sHighEnd = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the memory capacity distribution (r for random, c for clustered):");
Console.WriteLine("random");
sRandom = Convert.ToInt32(br.readLine());
Console.WriteLine("clustered");
sClustered = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the CPU capacity range of the slave nodes in increments of 10GHz");
sCpuCap = Convert.ToInt64(br.readLine());
Console.WriteLine("Enter the low end in GHz:");
scLowEnd = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the high end in GHz:");
scHighEnd = Convert.ToInt32(br.readLine());
Console.WriteLine("Enter the CPU capacity distribution (r for random, c for clustered):");
Console.WriteLine("random");
scRandom = Convert.ToInt32(br.readLine());
Console.WriteLine("clustered");
scClustered = Convert.ToInt32(br.readLine());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public virtual void simulation()
{
try
{
startTime = DateTimeHelperClass.CurrentUnixTimeMillis();
string[] newFile = SplitFile.splitFile(new File(data), 2);
Console.WriteLine("Slaves connected in sequencial");
NewFileSplit nfs = new NewFileSplit();
nfs.fileSplit(newFile[0], noOfSlaves / 2);
Console.WriteLine("Slaves connected in Parllel");
Filesplit fs = new Filesplit();
fs.fileSplit(newFile[1], noOfSlaves - (noOfSlaves / 2));
int fileCount = 0;
for (int i = 0; i < noOfSlaves - (noOfSlaves / 2); i++)
{
MyThread m = new MyThread(newFile[1] + "_" + ++fileCount, noOfSlaves - (noOfSlaves / 2));
m.Start();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public virtual void viewOutput()
{
try
{
Console.WriteLine("Volume: ....." + volume);
Console.WriteLine("Velocity: ....." + velocity);
Console.WriteLine("Master node memory low end: ....." + mLowEnd);
Console.WriteLine("Master node memory high end: ....." + mHighEnd);
Console.WriteLine("Master node CPU low end: ....." + mcLowEnd);
Console.WriteLine("Master node CPU high end: ....." + mcHighEnd);
Console.WriteLine("Number of slave nodes: ....." + noOfSlaves);
Console.WriteLine("Connectivity: 1:.....");
Console.WriteLine("Slave node memory capacity low end: ....." + sLowEnd);
Console.WriteLine("Slave node memory capacity high end: ....." + sHighEnd);
Console.WriteLine("Slave node memory capacity distribution: ....." + sRandom + ", " + sClustered);
Console.WriteLine("Slave node CPU capacity low end: ....." + sLowEnd);
Console.WriteLine("Slave node CPU capacity high end: ....." + scHighEnd);
Console.WriteLine("Slave node CPU capacity distribution: ....." + scRandom + ", " + scClustered);
endTime = DateTimeHelperClass.CurrentUnixTimeMillis();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public virtual void calculataThroughPut()
{
try
{
Thread.Sleep(5000);
// System.out.println("Throughput (conventional):");
double throughput = endTime - startTime;
double throughputCon = 300;
Console.WriteLine("ThroughPut (conventional)" + throughputCon);
Console.WriteLine("Throughput (bigdata):" + throughput);
Console.WriteLine("Speedup factor ((Thput_Bigdata - Thput_conv)/Thput_conv): ");
double result = ((throughput - throughputCon) / throughputCon);
Console.Write(result);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public static void Main(string[] args)
{
Master m = new Master();
Thread.Sleep(10000);
m.readInput();
m.simulation();
m.viewOutput();
m.calculataThroughPut();
}
Mythread.cs file is
Mythread.cs file is
using System;
using System.Threading;
using System.Runtime.Remoting;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
///
/// <summary>
/// @author Laxman
/// </summary>
public class MyThread : System.Threading.Thread
{
internal string filepath = "";
internal int noOfSlaves = 0;
//public MyThread(string filepath, int size)
//{
//this.filepath = filepath;
//this.noOfSlaves = size;
//}
public virtual void run()
{
File f = new File(filepath);
try
{
MasterInt m = new MasterImp();
m.updateSize(f.length());
m.setFile(filepath, noOfSlaves);
Naming.rebind("master", m);
string[] command = new string[] {"cmd.exe", "/C", "Start", "c://src//slave.bat"};
Runtime r = Runtime.Runtime;
try
{
Process p = r.exec(command);
p.waitFor();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
catch (Exception)
{
}
}
}
please help me.
推荐答案
Not surprisingly MS has a nice tutorial on threading: Threading Tutorial[^]
最好的问候
Espen Harlinn
Not surprisingly MS has a nice tutorial on threading: Threading Tutorial[^]
Best regards
Espen Harlinn
这篇关于如何在C#中创建线程以及如何调用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文