KarelThreadProgram3 (KarelOOP2)
Retired DISLab
/* * Copyright 2020 Sangwon Park @ DISLab, HUFS * * 스레드 프로그래밍 예제이다. * 세 개의 스레드를 만들어 실행시킨다. * 먼저 세 개의 캐럴을 만들고 각 스레드는 월드를 빙글빙글 돌아간다. * Thread 클래스를 계승한 클래스로 스레드를 만든다. */ package cp.java.week11.thread; import java.util.ArrayList; import hufs.dislab.karel.IKarel; import hufs.dislab.karel.IKarelProgram; import hufs.dislab.karel.SimpleKarel; class ThreadKarel extends SimpleKarel { public void run() { while (true) { if (frontIsClear()) move(); else turnLeft(); } } } @SuppressWarnings("serial") public class KarelThreadProgram3 extends IKarelProgram { ArrayList<Thread> thr = new ArrayList<Thread>(); @Override protected void onInit() { thr.add(new Thread(getIKarel())); IKarel karel = new ThreadKarel(); addIKarel(karel, 1, getWorldHeight(), SOUTH, 0); thr.add(new Thread(karel)); karel = new ThreadKarel(); addIKarel(karel, getWorldWidth(), getWorldHeight(), WEST, 0); thr.add(new Thread(karel)); } @Override protected void onStart() { for (Thread th : thr) th.start(); } public static void main(String[] args) { IKarelProgram.main(args, new ThreadKarel()); } }