오늘의 C# 공부 : 인터럽트 , 싱크로 나이즈

////인터럽트

//using System;
//using System.Security.Permissions;
//using System.Threading;

//namespace InterrruptingThread
//{
//    class SideTask
//    {
//        int count;

//        public SideTask(int count)
//        {
//            this.count = count;
//        }

//        public void KeepAlive()
//        {
//            try
//            {
//                Console.WriteLine(“Running thread isn’t gonna by interruped”);

//                Thread.SpinWait(1000000000);

//                while (count > 0)
//                {
//                    Console.WriteLine("{0} left", count–);

//                    Console.WriteLine(“Entering into WaitJoinSleep State…”);
//                    Thread.Sleep(10);
//                }
//                Console.WriteLine(“Count  ; 0”);
//            }

//            catch (ThreadInterruptedException e)
//            {
//                Console.WriteLine(e);
//            }

//            finally
//            {
//                Console.WriteLine(“Clearing resource…”);
//            }
//        }

//        class MainApp
//        {
//            static void Main(string[] args)
//            {
//                SideTask task = new SideTask(100);
//                Thread t1 = new Thread(new ThreadStart(task.KeepAlive));
//                t1.IsBackground = false;

//                Console.WriteLine(“Starting Thread….”);
//                t1.Start();

//                Thread.Sleep(100);

//                Console.WriteLine(“Interrupting thread…”);
//                t1.Interrupt();

//                Console.WriteLine(“Wating untile thread stops…”);
//                t1.Join();

//                Console.WriteLine(“Finished”);
//            }
//        }
//    }
//}

////synchronize/

//using System;
//using System.Threading;

//namespace Synchronize
//{
//    class Counter
//    {
//        const int LOOP_COUNT = 1000;

//        readonly object thisLock;

//        private int count;
//        public int Count
//        {
//            get { return count; }
//        }

//        public Counter()
//        {
//            thisLock = new Object();
//            count = 0;
//        }

//        public void Increse()
//        {
//            int loopCount = LOOP_COUNT;
//            while (loopCount– > 0)
//            {
//                lock (thisLock)
//                {
//                    count++;
//                }
//                Thread.Sleep(1);
//            }
//        }

//        public void Decrease()
//        {
//            int loopCount = LOOP_COUNT;
//            while (loopCount– > 0)
//            {
//                lock (thisLock)
//                {
//                    count–;
//                }
//                Thread.Sleep(1);
//            }
//        }
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Counter counter = new Counter();

//            Thread incThread = new Thread(new ThreadStart(counter.Increse));
//            Thread decThread = new Thread(new ThreadStart(counter.Decrease));

//            incThread.Start();
//            decThread.Start();

//            incThread.Join();
//            decThread.Join();

//            Console.WriteLine(counter.Count);
//        }
//    }
//}

////모니터 클래스로 동기화하기

//using System;
//using System.Threading;

//namespace UsingMoniter
//{
//    class Counter
//    {
//        const int LOOP_COUNT = 1000;

//        readonly object thisLock;

//        private int count;
//        public int Count
//        {
//            get { return count; }
//        }

//        public Counter()
//        {
//            thisLock = new object();
//            count = 0;
//        }

//        public void Increase()
//        {
//            int loopCount = LOOP_COUNT;

//            while(loopCount– >0)
//            {
//                Monitor.Enter(thisLock);
//                try
//                {
//                    count++;
//                }
//                finally
//                {
//                    Monitor.Exit(thisLock);
//                }
//                Thread.Sleep(1);
//            }
//        }

//        public void Decrease()
//        {
//            int loopCount = LOOP_COUNT;
//            while (loopCount– > 0)
//            {
//                Monitor.Enter(thisLock);
//                try
//                {
//                    count–;
//                }

//                finally
//                {
//                    Monitor.Exit(thisLock);
//                }
//                Thread.Sleep(1);
//            }
//        }
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Counter counter = new Counter();

//            Thread incThread = new Thread(new ThreadStart(counter.Increase));
//            Thread decThread = new Thread(new ThreadStart(counter.Decrease));

//            incThread.Start();
//            decThread.Start();

//            incThread.Join();
//            decThread.Join();

//            Console.WriteLine(counter.Count);
//        }
//    }
//}

//// 저수준 동기화

//using System;
//using System.Threading;

//namespace WaitPalse
//{
//    class Counter
//    {
//        const int LOOP_COUNT = 1000;
//        readonly object thisLock;

//        bool lockedCount = false;

//        private int count;
//        public int Count
//        {
//            get { return count; }
//        }

//        public Counter()
//        {
//            thisLock = new object();
//            count = 0;
//        }

//        public void Increase()
//        {
//            int loopCount = LOOP_COUNT;

//            while (loopCount– > 0)
//            {
//                lock (thisLock)
//                {
//                    while (count > 0 || lockedCount == true)
//                        Monitor.Wait(thisLock);

//                    lockedCount = true;
//                    count++;
//                    lockedCount = false;
//                    //Console.WriteLine(count);
//                    Monitor.Pulse(thisLock);
//                }
//            }
//        }

//        public void Decrese()
//        {
//            int loopCount = LOOP_COUNT;

//            while (loopCount– > 0)
//            {
//                lock (thisLock)
//                {
//                    while (count > 0 || lockedCount == true)
//                        Monitor.Wait(thisLock);

//                    lockedCount = true;
//                    count–;
//                    lockedCount = false;
//                    //Console.WriteLine(count);
//                    Monitor.Pulse(thisLock);
//                }
//            }
//        }

//        class MainApp
//        {
//            static void Main(string[] args)
//            {
//                Counter counter = new Counter();

//                Thread incThread = new Thread(new ThreadStart(counter.Increase));
//                Thread DecThread = new Thread(new ThreadStart(counter.Decrese));

//                incThread.Start();
//                DecThread.Start();

//                incThread.Join();
//                DecThread.Join();

//                Console.WriteLine(counter.Count);

//            }
//        }
//    }
//}

// task
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace UsingTask
{
    class MainApp
    {
        static void Main(string[] args)
        {
            string srcFile = args[0];

            Action FileCopyAction = (object state) =>
                {
                    String[] paths = (String[])state;
                    File.Copy(paths[0], paths[1]);

                    Console.WriteLine(“TaskID:{0} , TaskID:{1} ,{2} was copied to {3}”, Task.CurrentId, Thread.CurrentThread.ManagedThreadId, paths[0], paths[1]);
                };

            Task t1 = new Task(
                FileCopyAction, new string[] {srcFile, srcFile+".copy1"});

            Task t2 = Task.Factory.StartNew(FileCopyAction, new string[] {srcFile, srcFile+ “.copy2”});

            t1.Start();

            Task t3 = new Task(
                FileCopyAction, new string[] { srcFile, srcFile + “.copy3” }
                );

            t3.RunSynchronously();

            t1.Wait();
            t2.Wait();
            t3.Wait();

        }
    }
}

Hugo로 만듦
JimmyStack 테마 사용 중