import java.awt.*; import java.awt.event.*; import java.util.*; // 쓰레드 클래스를 상속 받아서 run을 오버라이딩 할 것이다. public class SimpleThread extends Thread{ static Canvas canvas; int y = 0; String str = null; Random rand = new Random(); public SimpleThread(int y, String str){ this.y = y; this.str = str; } // run은 start를 통해서 실행 된다. 현재 run은 오버라이딩 한 상태다. public void run(){ Graphics g = canvas.getGraphics(); g.drawString(str, 20, y); for(int i=0;i<300;i++){ try{ Thread.sleep(100); i += rand.nextInt(5); //각 말들은 1초 간격으로 0부터 5 까지의 랜덤한 숫자로 달리게 된다. if(i > 300){ i = 300; } g.fillRect(50, y, i, 20); }catch(Exception e){ } } } public static void main(String args[]){ Frame frm = new Frame("쓰레드 테스트"); canvas = new Canvas(); frm.setSize(400, 400); frm.add(canvas); frm.setVisible(true); SimpleThread test1 = new SimpleThread(30,"1번 말"); SimpleThread test2 = new SimpleThread(70,"2번 말"); SimpleThread test3 = new SimpleThread(110,"3번 말"); SimpleThread test4 = new SimpleThread(150,"4번 말"); SimpleThread test5 = new SimpleThread(190,"5번 말"); SimpleThread test6 = new SimpleThread(230,"6번 말"); SimpleThread test7 = new SimpleThread(270,"7번 말"); test1.start(); test2.start(); test3.start(); test4.start(); test5.start(); test6.start(); test7.start(); frm.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } }
'노트 > 소스 코드' 카테고리의 다른 글
[R] Co-occurrence Matrix (0) | 2016.02.09 |
---|---|
[Java] Polynomials with integer coefficients (0) | 2016.01.18 |
[C] Multi Thread Server Example (0) | 2011.12.28 |
[Java] Twitter Capturer (with JSON) (NOT BRUSH) (0) | 2011.12.28 |
[C++] Dijkstra Algorithm (NOT BRUSH) (0) | 2011.12.28 |
[C] English Morpheme Analyzer (With Lex) (0) | 2011.12.28 |
[C] Char, Word, Line Count (With Lex) (0) | 2011.12.28 |
[JavaScript] Layer Popup Example (0) | 2011.12.28 |
[Java] Notepad (0) | 2011.12.28 |
[Java] RSS Reader (0) | 2011.12.28 |