//********************************************************************* //* Programmer : Jang Kyu-O(Àå ±Ô¿À) * //* Date : 1996/2/10 * //* E-Mail : kojang@ctkhost.ctk.co.kr * //* Tel : +82-02-3149-4821 * //* Address : Á¦À̾¾Çö ¿¤¸²³×Æ®»ç¾÷º»ºÎ À¥ÆÀ * //********************************************************************* import java.applet.*; import java.awt.*; import java.lang.*; import java.io.*; class BarInfo { private boolean canAccess = false; private int size=50; public synchronized void init() { canAccess = false; size = 50; } public int getSize(){ return size;}; public synchronized int smallerSize() { while(canAccess == false ) { try { wait(); } catch(Exception e) { } } size -=10; canAccess = false; System.out.println("smaller..."); notify(); return size; } public synchronized void largerSize() { while(canAccess == true ) { try { wait(); } catch(Exception e) { } } size += 10; canAccess = true; System.out.println("larger !!!"); notify(); } } class LargerBarSize extends Thread { BarInfo info; LargerBarSize(BarInfo i) { info = i; } public void run() { for (int i = 0; i < 100; i++) { info.largerSize(); try { sleep( (int)(Math.random()*100)); } catch(Exception e) { } } } } class SmallerBarSize extends Thread { BarInfo info; SmallerBarSize(BarInfo i) { info = i; } public void run() { for (int i = 0; i < 100; i++) { info.smallerSize(); try { sleep( (int)(Math.random()*200)); } catch(Exception e) { } } } } public class BarThreadTest extends Applet implements Runnable { Thread updateThread; BarInfo info; LargerBarSize larger=null; SmallerBarSize smaller=null; Image tmpimg; Graphics tmpmap; boolean isbegin=true; public void init() { info = new BarInfo(); } public boolean mouseDown(Event evt, int x, int y) { if(updateThread != null) updateThread = null; if(larger != null) larger = null; if(smaller != null) smaller = null; info.init(); updateThread = new Thread(this); larger = new LargerBarSize(info); smaller = new SmallerBarSize(info); updateThread.start(); larger.start(); smaller.start(); return true; } public void stop() { if (updateThread.isAlive()) { updateThread.stop(); updateThread = null; } if (larger.isAlive()) { larger.stop(); larger = null; } if (smaller.isAlive()) { smaller.stop(); smaller = null; } } public void run() { while (updateThread != null) { repaint(); try { updateThread.sleep(10); } catch (InterruptedException e) { } } } public void paint(Graphics g) { Rectangle r = bounds(); if(isbegin==true) { tmpimg = createImage(r.width, r.height); tmpmap = tmpimg.getGraphics(); isbegin = false; } update(g); } public void update(Graphics g) { tmpmap.setColor(Color.lightGray); tmpmap.fillRect(0, 0, size().width, size().height); if(larger!=null && smaller!=null) { tmpmap.setColor(Color.red); tmpmap.fill3DRect(10, 50, info.getSize(), 20, true); tmpmap.setColor(Color.blue); tmpmap.drawString("|", 10, 80); tmpmap.drawString("|", 60, 80); tmpmap.drawString("|", 110, 80); tmpmap.drawString("|", 110, 80); tmpmap.drawString("|", 160, 80); tmpmap.drawString("|", 210, 80); tmpmap.drawString("0", 10, 100); tmpmap.drawString("50", 60, 100); tmpmap.drawString("100", 110, 100); tmpmap.drawString("150", 110, 100); tmpmap.drawString("200", 160, 100); tmpmap.drawString("250", 210, 100); tmpmap.setFont(new Font("Helvista", 1, 20)); tmpmap.setColor(new Color(0, 0, 170)); tmpmap.drawString("Larger bar size...", 10, 150); } else { tmpmap.setFont(new Font("Helvista", 1, 20)); tmpmap.setColor(new Color(0, 0, 170)); tmpmap.drawString("Press mouse button...", 10, 150); } g.drawImage(tmpimg, 0, 0, this); } }