實驗--多執行緒

iamzxf發表於2015-06-05

實驗目標:

1、掌握執行緒和多執行緒的概念

2、掌握建立執行緒的兩種方法及其區別

3、瞭解執行緒的啟動、終止、同步、互斥和優先順序等概念

實驗內容:

1、編寫一個程式,其功能是執行之後,其中有一個執行緒可以輸出20次你的學號,另一個執行緒會輸出20次你的姓名。

思考:如何實現列印所有的學號之後再列印姓名?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;


namespace ConsoleApplication1
{
    class MyThread
    { 
        private string id="250129131";
        private string name = "zhang";
        private int flag;

        public MyThread(int ff)
        {
            flag = ff;
        }

        public void disp()
        {
              if (flag == 0)
                {
                    for (int j = 0; j < 20; j++)
                    {
                        Console.WriteLine(id);
                        Thread.Sleep(20);
                    }
                }
                else
                {
                    for (int j = 0; j < 20; j++)
                    {
                        Console.WriteLine(name);
                        Thread.Sleep(20);
                    }
                }            
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyThread thread1 = new MyThread(1);
            MyThread thread2 = new MyThread(0);

            Thread objThread1 = new Thread(new ThreadStart(thread1.disp));
            Thread objThread2 = new Thread(new ThreadStart(thread2.disp));

            objThread1.Start();
            objThread1.Join();
            objThread2.Start();

            Console.ReadLine();

        }
    }
}

2、編寫程式實現火車票售票程式。要求如下:

(1)假設列車的座號為1-60,車票按照座號依次售出

(2)有四個視窗可以同時售票(即:建立四個執行緒)

(3)每個視窗都有20個人排隊買票

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
{
    class MyThread
    {
        static int count = 1;
        public void sellTic()
        {          
                for (int i = 0; i < 20; i++)
                {
                    lock (this)
                    {
                        if (count > 60)
                            Console.WriteLine("{0}的第{1}個人來買票,票已售完!",Thread.CurrentThread.Name,i+1);
                        else
                        {

                            Console.WriteLine("{0}的第{1}個人來買票,得到了第{2}號票!", Thread.CurrentThread.Name, i+1,count);
                            count++;
                            Thread.Sleep(10);

                        }
                    }                
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyThread obj = new MyThread();
            Thread th1 = new Thread(new ThreadStart(obj.sellTic));
            th1.Name = "視窗1";
            Thread th2 = new Thread(new ThreadStart(obj.sellTic));
            th2.Name = "視窗2";
            Thread th3 = new Thread(new ThreadStart(obj.sellTic));
            th3.Name = "視窗3";
            Thread th4 = new Thread(new ThreadStart(obj.sellTic));
            th4.Name = "視窗4";

            th1.Start();            
            th2.Start();            
            th3.Start();            
            th4.Start();
            

            Console.ReadLine();
        }
    }
}


相關文章