While it is against our religious beliefs to cavil over trivia, it is also essential to know of some Tid-bit functions that come along with threads. They might come in handy when threads try to play hob with you. Let us check out the first one, which, infact, is the solution to an array of problems.
The toString() does the work of converting the relevant details into a string form. Here we use toString() with t, so the 'relevant details' are the details of the thread t. When this converted string is specified as a parameter to the showStatus, the value is displayed on the show status.import java.applet.*; import java.awt.*; public class zzz extends Applet implements Runnable { public void run() { } public boolean mouseUp(Event e, int x, int y) { Thread t; t = new Thread(this); showStatus(t.toString()); return true; } }
The list(), (which is the only new thing introduced here - in case you are wondering about the necessity of the above program), tells you what are the components of the current window and what are the threads currently executed. Don't despair. The list() will become clearer when we get to the next chapter on AWT.21 import java.applet.*; import java.awt.*; public class zzz extends Applet implements Runnable { public void run() { } public boolean mouseUp(Event e, int x, int y) { Thread t; t = new Thread(this); t.start(); list(); return true; } }
The ThreadGroup() will list the different threads executed at the moment when clicked with the mouse. But the hitch is that this output is available only in the DOS shell,i.e., if you switch to the DOS shell from the Windows95 environment.22 import java.applet.*; import java.awt.*; public class zzz extends Applet implements Runnable { public void run() { } public boolean mouseUp(Event e, int x, int y) { Thread t; t = new Thread(this); showStatus(t.getThreadGroup().toString()); t.getThreadGroup().list(); return true; } }
The super() will give a name to the thread (whatever provided as the parameter will be the name given to the thread). In the ttt class, we name it as "good". In the mouseDown(), we set the name of the thread u to "bad". So, now the run(), which is supposed to show the name of the thread will alternate between "good" and "bad".23 import java.applet.*; import java.awt.*; class ttt extends Thread { zzz a;int i=0; ttt( zzz z) { super("good"); a = z; } public void run() { while ( true) { i++; a.repaint(); a.showStatus(getName()); } } } class uuu extends Thread { zzz a;int j=0; uuu(zzz z) { a = z; } public void run() { while ( true) { a.showStatus(getName()); j++; a.repaint(); } } } public class zzz extends Applet { uuu u; ttt t; public void init() { u = new uuu (this); t = new ttt (this); t.start(); u.start(); } public boolean mouseDown(Event e, int x,int y) { u.setName("bad"); return true; } }
24 import java.applet.*; import java.awt.*; public class zzz extends Applet implements Runnable { public void run() { } public boolean mouseUp(Event e, int x, int y) { Thread t; t = new Thread(this, "good"); showStatus(t.getName()); return true; } }
The yield()
As must be evident, while creating the new thread t, we are also giving it the name of "good". This is another way of giving your thread a name. Now when we show the status of the thread, the getName() will display "good".
import java.applet.*; import java.awt.*; class ttt extends Thread { zzz a; int i=0; ttt( zzz z) { a = z; } public void run() { while ( true) { yield(); i++; a.repaint(); a.showStatus(getName()); } } } class uuu extends Thread { zzz a;int j=0; uuu( zzz z) { a = z; } public void run() { while ( true) { j++; a.repaint(); a.showStatus(getName()); } } } public class zzz extends Applet { uuu u; ttt t; public void start() { u = new uuu (this); t = new ttt (this); t.start(); u.start(); } public void paint(Graphics g) { g.drawString("i .." + t.i,1,20); g.drawString("j .." + u.j,1,60); } }