Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

행복한 개구리

C#21.03.29.복습 본문

C#/복습

C#21.03.29.복습

HappyFrog 2021. 3. 29. 21:56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Homework1234
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            Thread t;

            ThreadStart ts = new ThreadStart(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("Hello~");
                    Thread.Sleep(500);
                }
            });
            t = new Thread(ts);

            t.Start();

            Thread t2 = new Thread(new ThreadStart(this.Hi));

            t2.Start();
        }

        private void Hi()
        {
            for(int i = 0; i < 10; i++)
            {
                Console.WriteLine("Hi");
                Thread.Sleep(500);
            }
        }
    }
}

============================================================================

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

namespace Homework1234
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            Thread t = new Thread(new ThreadStart(this.TestThread));
            t.Start();

            Thread.Sleep(3000);
            t.Abort("Information from Main.");
        }

        private void TestThread()
        {
            try
            {
                while (true)
                {
                    Console.WriteLine("Thread is running...");
                    Thread.Sleep(1000);
                }
            }
            catch(ThreadAbortException abortException)
            {
                Console.WriteLine((string)abortException.ExceptionState);
            }
        }
    }
}

============================================================================

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

namespace Homework1234
{
    class App
    {
        Thread thread1;
        Thread thread2;
        public App()
        {
            Console.WriteLine("App");

            thread1 = new Thread(()=>{
                Console.WriteLine(thread1.ThreadState);
                Thread.Sleep(100);
                thread1.Abort();
                Thread.Sleep(100);                
            });

            thread1.Name = "Thread1";

            Console.WriteLine(thread1.ThreadState);
            thread1.Start();

            thread2 = new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(100);
                    Console.WriteLine(thread1.ThreadState);
                }
            });
            thread2.Start();
        }
    }
}

============================================================================

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

namespace Homework1234
{
    public class Toast
    {
    }
    public class Egg
    {
    }
    class App
    {
       
        public App()
        {
            Console.WriteLine("App");

            Run();
        }

        public async void Run()
        {
            Task<Toast> toastTask = MakeToast(2);
            Task<Egg> eggTask = this.MakeEggs();
            eggTask.Wait();
            Console.WriteLine(eggTask.Result);

            Task.WaitAll(new Task[] { toastTask, eggTask });
            Console.WriteLine("{0}, {1}", toastTask.Result, eggTask.Result);

            Task[] arr = new Task[] { toastTask, eggTask };
            var idx = Task.WaitAny(arr);
            Console.WriteLine("idx: {0}", idx);

            Toast[] toasts = await Task.FromResult(Enumerable.Range(1, 5).Select(x => new Toast()).ToArray());
            foreach (var toast in toasts)
            {
                Console.WriteLine(toast);
            }
        }

        private async Task<Egg> MakeEggs()
        {
            Console.WriteLine("달걀을 삶습니다.");
            await Task.Delay(2000);
            Console.WriteLine("달걀이 다 삶아 졌습니다.");
            return new Egg();
        }

        private async Task<Toast> MakeToast(int num)
        {
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("토스트기에 빵을 넣었습니다.");
            }
            await Task.Delay(2000);
            Console.WriteLine("빵이 탔습니다.");
            await Task.Delay(1000);
            Console.WriteLine("빵을 꺼냈습니다.");
            return new Toast();
        }

    }
}

이 친구도 자주 봐야할 것 같은 느낌이 든다.

============================================================================

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

namespace Homework1234
{
    public class Egg
    {
        public string desc;
        public Egg(string desc)
        {
            this.desc = desc;
        }
    }
    
    class App
    {
       
        public App()
        {
            Console.WriteLine("App");

            Task<Egg> taskEgg = this.CookEgg(new Egg("생달걀"));
            taskEgg.Wait();
            Console.WriteLine(taskEgg.Result.desc);
        }

        private async Task<Egg> CookEgg(Egg egg)
        {
            var boiledEgg = await BoilEgg(egg);
            return await SeasonEgg(boiledEgg);
        }

        private async Task<Egg> BoilEgg(Egg egg)
        {
            Console.WriteLine("달걀을 삶기 시작합니다.");
            await Task.Delay(1000);
            egg.desc = "삶은달걀";
            Console.WriteLine("달걀이 삶아 졌습니다.");
            return egg;
        }

        private async Task<Egg> SeasonEgg(Egg egg)
        {
            await Task.Delay(500);
            egg.desc = "양념된 삶은 달걀";
            return egg;
        }

    }
}

알 것도 같고... 잘 모르겠기도 하고...

============================================================================

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

namespace Homework1234
{
    class DynamicArray
    {
        private int[] arr;
        private int capacity;
        private const int GROWTH_FACTOR = 2;
        private int count;

        public DynamicArray(int capacity = 2)
        {
            this.capacity = capacity;
            this.arr = new int[this.capacity];
            this.count = 0;
        }

        public void Add(int element)
        {
            
            if (this.count >= capacity)
            {
                int newSize = this.capacity * GROWTH_FACTOR;
                int[] temp = new int[newSize];
                for(int i = 0; i < this.arr.Length; i++)
                {
                    temp[i] = arr[i];
                }
                this.arr = temp;
                this.capacity = newSize;
            }
            this.arr[count] = element;
            count++;
        }

        public void Print()
        {
            for(int i = 0; i < this.arr.Length; i++)
            {
                if(this.arr[i] == 0)
                {
                    break;
                }
                Console.WriteLine(this.arr[i]);
            }
        }
    }

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

namespace Homework1234
{    
    class App
    {
       
        public App()
        {
            Console.WriteLine("App");

            DynamicArray dA = new DynamicArray(2);
                        
            for(int i = 0; i < 20; i++)
            {
                dA.Add(i + 1);                
            }
            dA.Print();
        }
    }
}

학원에서 내가 했던거랑 다른 방식이어서 살짝 손 봤다.

============================================================================

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

namespace Homework1234
{
    public class SingleLinkedList
    {
        private Node head;

        public SingleLinkedList()
        {

        }

        public void Add(Node node)
        {
            if (this.head == null)
            {
                this.head = node;
            }
            else
            {
                Node current = this.head;
                while (current != null && current.next != null)
                {
                    current = current.next;
                }
                current.next = node;
            }
        }

        public int Count()
        {
            int count = 0;
            Node current = this.head;
            while (current != null)
            {
                count++;
                current = current.next;
            }
            return count;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1234
{
    public class Node
    {
        public int data;
        public Node next;

        public Node(int data)
        {
            this.data = data;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Homework1234
{ 

    class App
    {
       
        public App()
        {
            Console.WriteLine("App");
            SingleLinkedList list = new SingleLinkedList();

            Node node1 = new Node(1);
            Node node2 = new Node(2);
            Node node3 = new Node(3);
            Node node4 = new Node(4);

            list.Add(node1);
            list.Add(node2);
            list.Add(node3);
            list.Add(node4);

            Console.WriteLine(list.Count());
        }
    }
}

오늘은 여기까지.

 

'C# > 복습' 카테고리의 다른 글

C# 21.04.05.복습  (0) 2021.04.05
C#21.03.26~28.복습  (0) 2021.03.26
C# 21.03.25.복습  (0) 2021.03.25
C# 21.03.23.복습  (0) 2021.03.23