using System;
using System.Collections.Generic;
using System.Threading;
namespace HelloWorld
{
public class Minerals
{
private int amount = 100;
public int Amount
{
get
{
return this.amount;
}
set
{
this.amount = value;
}
}
}
public class App
{
private Minerals minerals = new Minerals();
private object lockobj = new object();
public App()
{
List<SCV> list = new List<SCV>();
for (int i = 0; i < 10; i++)
{
SCV scv = new SCV(i, minerals, lockobj);
list.Add(scv);
}
List<Thread> threads = new List<Thread>();
foreach (SCV scv in list)
{
Thread t = new Thread(scv.Work);
t.Start();
Thread.Sleep(10);
threads.Add(t);
}
foreach (var t in threads)
{
t.Join();
}
Console.WriteLine("minerals: {0}", this.minerals.Amount);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class SCV
{
private int num;
private Minerals mineral;
private object lockobj;
//생성자
public SCV(int x, Minerals mineral, object lockobj)
{
this.num = x;
this.mineral = mineral;
this.lockobj = lockobj;
}
public void Work()
{
Console.WriteLine("SCV{0}번이 미네랄을 캡니다. 접근하는 미네랄: {1}", this.num, this.mineral.Amount);
this.mineral.Amount -= 8;
}
}
}
'C# > 수업 과제' 카테고리의 다른 글
if문 문제만들어서 풀기 (0) | 2021.12.24 |
---|---|
if문 (0) | 2021.12.24 |
OX퀴즈 (0) | 2021.12.24 |
탱크와 벌쳐 전투 (0) | 2021.12.24 |
건물건설과 유닛생산 (0) | 2021.12.24 |