본문 바로가기
Study/C#

[C#][Study][기초다지기] lock 문

by 스테디코디스트 2023. 9. 23.
반응형

lock 문

- 현재 thread에서 프로그램을 실행하는동안 다른 thread의 개체를 잠그고, 진행되던 thread의 실행이 끝난 뒤에 개체의 잠금이 해제되어 다음 thread를  실행할 수 있게 함

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Lock_Statement
{
    class Program
    {
        public void printname()
        {
            Console.WriteLine("My name is Steven Clark");
        }
 
        static void Main(string[] args)
        {
            Program p = new Program();
            
            lock (p) // lock 구문 생성 -> 이 실행이 끝날 때까지 다른 thread는 사용 불가
            {
                p.printname();
            }
            Console.ReadLine();
        }
    }
}