今天的状况不太好,心中老是怀念各科老师所布置的作业,呵呵,几乎每次交作业我都会发出怪叫。没办法,个人患有轻微的健忘症。在同学的眼中,我真是一个怪物。少见多怪嘛,要是在高一级的大学就不会这样了。C#程序运行了一大堆,通过,错误,找错误,再通过,循环。脑子有点晕,没办法,谁叫我喜欢这些东西呢。对于线程问题,我还需要努力,多找几本书看看,或许能够攻克。代表真烦恼,老是摸不着头脑。还是那句老话,多找几本书看看,不信自己真的就那么苯。一、二、三、四......
//ThreadLockApp.cs
using System;
using System.Threading;
class Database
{
public void SaveData(string text)
{
lock(this)
{
Console.WriteLine("Database.SaveData - Started");
Console.WriteLine("Database.SaveData - Working");
for(int i=1;i<50;i++)
{
Console.Write(text);
}
Console.WriteLine("/nDatabase.SaveData - Ended");
}
}
}
class ThreadLockApp
{
public static Database db=new Database();
public static void WorkerThreadMethod1()
{
Console.WriteLine("Worker thread #1 - Started");
Console.WriteLine("Worker thread #1 - calling Database.SaveData");
db.SaveData("X");
Console.WriteLine("Worker thread #1 - Returned from Output");
}
public static void WorkerThreadMethod2()
{
Console.WriteLine("Worker thread #2 - Started");
Console.WriteLine("Worker thread #2 - Calling Database.SaveData");
db.SaveData("O");
Console.WriteLine("Worker thread #2 - Returned from Output");
}
public static void Main()
{
ThreadStart worker1=new ThreadStart(WorkerThreadMethod1);
ThreadStart worker2=new ThreadStart(WorkerThreadMethod2);
Console.WriteLine("Main - Creating worker threads");
Thread t1=new Thread(worker1);
Thread t2=new Thread(worker2);
t1.Start();
t2.Start();
}
}
//################################################################
//ThreadMonitor1App.cs
using System;
using System.Threading;
class Database
{
public void SaveData(string text)
{
Console.WriteLine("Database.SaveData - Started");
Console.WriteLine("Database.SaveData - Working");
for(int i=1;i<50;i++)
{
Console.Write(text);
}
Console.WriteLine("/nDatabase.SaveData - Ended");
}
}
class ThreadMonitor1App
{
public static Database db=new Database();
public static void WorkerThreadMethod1()
{
Console.WriteLine("Worker thread #1 - Started");
Console.WriteLine("Worker thread #1 - calling Database.SaveData");
db.SaveData("x");
Console.WriteLine("Worker thread #1 - Returned from Output");
}
public static void WorkerThreadMethod2()
{
Console.WriteLine("Worler thread #2 - Started");
Console.WriteLine("Worker thread #2 - calling Database.SaveData");
db.SaveData("o");
Console.WriteLine("Worker thread #2 - Returned from Output");
}
public static void Main()
{
ThreadStart worker1=new ThreadStart(WorkerThreadMethod1);
ThreadStart worker2=new ThreadStart(WorkerThreadMethod2);
Console.WriteLine("Main - Creating worker threads");
Thread t1=new Thread(worker1);
Thread t2=new Thread(worker2);
t1.Start();
t2.Start();
}
}
//###########################################################
//ThreadMonitor2App.cs
using System;
using System.Threading;
class Database
{
public void SaveData(string text)
{
Monitor.Enter(this);
Console.WriteLine("Database.SaveData - Started");
Console.WriteLine("Database.SaveData - Working");
for(int i=1;i<50;i++)
{
Console.Write(text);
}
Console.WriteLine("/nDatabase.SaveData - Ended");
Monitor.Exit(this);
}
}
class ThreadMonitor2App
{
public static Database db=new Database();
public static void WorkerThreadMethod1()
{
Console.WriteLine("Worker thread #1 - Started");
Console.WriteLine("Worker thread #1 - calling Database.SaveData");
db.SaveData("X");
Console.WriteLine("Worker thread #1 - Returned from Output");
}
public static void WorkerThreadMethod2()
{
Console.WriteLine("Worker thread #2 - Started");
Console.WriteLine("Worker thread #2 - Calling Database.SaveData");
db.SaveData("O");
Console.WriteLine("Worker thread #2 - Returned from Output");
}
public static void Main()
{
ThreadStart worker1=new ThreadStart(WorkerThreadMethod1);
ThreadStart worker2=new ThreadStart(WorkerThreadMethod2);
Console.WriteLine("Main - Creating worker threads");
Thread t1=new Thread(worker1);
Thread t2=new Thread(worker2);
t1.Start();
t2.Start();
}
}
//#############################################################
//ThreadMutexApp.cs
using System;
using System.Threading;
class Database
{
Mutex mutex=new Mutex(false);
public void SaveData(string text)
{
mutex.WaitOne();
Console.WriteLine("Database.SaveData - Started");
Console.WriteLine("Database.SaveData - Working");
for(int i=1;i<50;i++)
{
Console.Write(text);
}
Console.WriteLine("/nDatabase.SaveData - Ended");
mutex.Close();
}
}
class ThreadMutexApp
{
public static Database db=new Database();
public static void WorkerThreadMethod1()
{
Console.WriteLine("Worker thread #1 - Started");
Console.WriteLine("Worker thread #1 - calling Database.SaveData");
db.SaveData("X");
Console.WriteLine("Worker thread #1 - Returned from Output");
}
public static void WorkerThreadMethod2()
{
Console.WriteLine("Worker thread #2 - Started");
Console.WriteLine("Worker thread #2 - Calling Database.SaveData");
db.SaveData("O");
Console.WriteLine("Worker thread #2 - Returned from Output");
}
public static void Main()
{
ThreadStart worker1=new ThreadStart(WorkerThreadMethod1);
ThreadStart worker2=new ThreadStart(WorkerThreadMethod2);
Console.WriteLine("Main - Creating worker threads");
Thread t1=new Thread(worker1);
Thread t2=new Thread(worker2);
t1.Start();
t2.Start();
}
}
//##########################################################
//ThreadSchedule1App.cs
using System;
using System.Threading;
class ThreadSchedule1App
{
public static void WorkerThreadMethod1()
{
Console.WriteLine("Worker thread started");
Console.WriteLine("Worker thread - counting slowly from 1 to 5");
for(int i=1;i<6;i++)
{
for(int j=0;j<9;j++)
{
Console.Write(".");
// Code to imitate work being done.
}
Console.Write("{0}",i);
}
Console.WriteLine("/nWorker thread finished");
}
public static void WorkerThreadMethod2()
{
Console.WriteLine("Worker thread started");
Console.WriteLine("Worker thread - counting slowly from 6 to 11");
for(int i=6;i<12;i++)
{
for(int j=0;j<9;j++)
{
Console.Write(".");
// Code to imitate work being done.
}
Console.Write("{0}",i);
}
Console.WriteLine("/nWorker thread finished");
}
public static void Main()
{
ThreadStart worker1=new ThreadStart(WorkerThreadMethod1);
ThreadStart worker2=new ThreadStart(WorkerThreadMethod2);
Console.WriteLine("Main - Creating worker threads");
Thread t1=new Thread(worker1);
Thread t2=new Thread(worker2);
t1.Start();
t2.Start();
}
}
//##############################################################
//ThreadSchedule2App.cs
using System;
using System.Threading;
class ThreadSchedule1App
{
public static void WorkerThreadMethod1()
{
Console.WriteLine("Worker thread started");
Console.WriteLine("Worker thread - counting slowly from 1 to 5");
for(int i=1;i<6;i++)
{
for(int j=0;j<9;j++)
{
Console.Write(".");
// Code to imitate work being done.
}
Console.Write("{0}",i);
}
Console.WriteLine("/nWorker thread finished");
}
public static void WorkerThreadMethod2()
{
Console.WriteLine("Worker thread started");
Console.WriteLine("Worker thread - counting slowly from 6 to 11");
for(int i=6;i<12;i++)
{
for(int j=0;j<9;j++)
{
Console.Write(".");
// Code to imitate work being done.
}
Console.Write("{0}",i);
}
Console.WriteLine("/nWorker thread finished");
}
public static void Main()
{
ThreadStart worker1=new ThreadStart(WorkerThreadMethod1);
ThreadStart worker2=new ThreadStart(WorkerThreadMethod2);
Console.WriteLine("Main - Creating worker threads");
Thread t1=new Thread(worker1);
Thread t2=new Thread(worker2);
t2.Priority=ThreadPriority.Highest;
t1.Priority=ThreadPriority.Lowest;
t1.Start();
t2.Start();
}
}