/* SimpleBanner.java _ ThreadBeep.java J Applet */ /** *+ * @author Jane */ import java.awt.*; import javax.swing.*; //Creates class containing two strings a thread and a volatile boolean public class BannerThreadBeep extends JApplet implements Runnable{ String msg="Moving Banner"; String msg2="123456 7890"; Thread t1=null; volatile boolean isSuspended=false; //Init is called by the browser as soon as the file runs //this constructor registers the components public void init(){ this.getContentPane().setBackground(Color.blue);t1=new Thread(this,"myThread1"); //start is called, which puts the thread into a 'wait' state t1.start(); System.out.println("out of init how many times is init called"); //init is called once } //start causes the //Java Virtual Machine to the run method of this thread public void start(){ System.out.println("in start When called"); synchronized(this){ isSuspended=false; //Thread t1 waits for another thread to invoke notify notify(); } } public void stop(){ System.out.println("in stop how is stop called"); isSuspended=true; } public void destroy(){ System.out.println("in destroy when is destroy called"); //destroy is called when the time period set has passed t1.interrupt(); if(t1!=null){ t1=null; } } //initialize a volatile character volatile char ch='Z'; public void run(){ for( ; ; ){ if(t1==null){break;} // try thread sleeping for 450 milliseconds, if isSuspended (the volatile booleand) is false, this //code will not run and will just to the catch InterruptedException try{ Thread.sleep(450); synchronized(this){ if(isSuspended==true){ String s=Thread.currentThread().getName(); System.out.println("thread "+s+"going to wait()"); wait(); System.out.println(""+Thread.currentThread().getName()+"coming out of wait()"); } } ch=msg.charAt(0); t1.sleep(0,3); msg=msg.substring(1,msg.length()); msg+=ch; repaint(); }catch(InterruptedException e){ System.out.println("in catch for Interrupted Exception how did we get here?"); //because isSuspended is false t1=null; break; } } System.out.println("leaving run()"); System.out.println("you can't sleepp the thread because the interrupted flag has been set"); if(t1.isInterrupted()){ System.out.println("is Interrupted()==true"); } } private Toolkit tk=Toolkit.getDefaultToolkit(); public void paint(Graphics g){ super.paint(g); g.drawString(msg, 50,50); g.drawString(msg2, 50,70); tk.beep(); } } /* int x=0, a=0; try{ System.out.println("in try 1"); x=System.in.available(); System.out.println("in try 2"); a=System.in.read(); System.out.println(" "+x+" "+a); }catch(Exception e){ System.out.println("Exception e "+e); } System.out.println("leaving run() again"+x+" "+a); try{ Thread.sleep(8*1000); }catch(InterruptedException e){} */