Java 8일차 Thread 테스트3

/* Thread를 이용한 경마게임 */

public class ThreadTest3 implements Runnable
{
String name;
int num;

public void run()
{
while(true)
{
try
{
if(num>50)
{
System.out.println(name+"통과");
break;
}

Thread.sleep(100);
num += (int)(Math.random()*4)+1;
System.out.println(name+" "+num);
}

catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

public ThreadTest3(String data)
{
name = data;
}

public static void main(String[] args)
{
ThreadTest3 tt1 = new ThreadTest3("1번");
ThreadTest3 tt2 = new ThreadTest3("2번");
ThreadTest3 tt3 = new ThreadTest3("3번");

new Thread(tt1).start();
new Thread(tt2).start();
new Thread(tt3).start();
}
}

댓글 없음: