Now that we have wrestled the various paraphernalia of threads to the mat, the rest will be toothless (and that is true for the most part). We have used threads to display images, do animation that works faster than your average banana slug, and even had two threads toil at the same time. But you have probably never bothered to wonder how two threads can communicate with each other. That's a hairy question, even if you have spent your entire adult life contemplating about mass-communications! :) Anyway, here's some scoop on how that's possible. Only the explanation is absconding right now. We'll get it here soon.
Cryptic Clue : There are two functions called wait() and the notify() . We have some examples furnished here. Try them. Bet you'll figure out half the stuff!
Thanx for visiting this page. Hope it will egg you on to get back to us. See you!
z2.javaimport java.awt.*; import java.applet.*; public class zzz extends Applet implements Runnable { Thread t; int i=0; public void init() { t=new Thread(this); t.start(); } public synchronized void run() { while(true) { i++; showStatus("Value.of i...."+i); try { wait(); } catch(Exception e) { } } } public synchronized boolean mouseUp(Event e, int x, int y) { notify(); return true; } }
z4.javaimport java.awt.*; import java.applet.*; class uuu extends Thread { zzz b; uuu(zzz a) { b = a; } public void run() { int k = 0; while (k < 1000) { b.j++; b.repaint(); k++; } } } class ttt extends Thread { zzz p; ttt(zzz q) { p = q; } public void run() { p.aa(); while (true) { p.i++; p.repaint(); } } } public class zzz extends Applet { ttt t; int i = 0; uuu u; int j=0; public void aa() { try wait(); catch ( Exception e) {} } public void init() { t = new ttt(this); t.start(); u = new uuu(this); u.start(); } public void paint(Graphics g) { g.drawString ("Value of i in ttt " + i,1,10); g.drawString ("Value of j in uuu " + j,1,40); } }
z5.javaimport java.awt.*; import java.applet.*; class uuu extends Thread { zzz b; uuu(zzz a) { b= a; } public void run() { int k = 0; while (k < 1000) { b.j++; b.repaint(); k++; } b.bb(); } } class ttt extends Thread { zzz p; ttt(zzz q) { p = q; } public void run() { p.aa(); while (true) { p.i++; p.repaint(); } } } public class zzz extends Applet { ttt t; int i = 0; uuu u; int j=0; public synchronized void bb() { notify(); } public synchronized void aa() { try wait(); catch ( Exception e) {} } public void init() { t = new ttt(this); t.start(); u = new uuu(this); u.start(); } public void paint(Graphics g) { g.drawString ("Value of i in ttt " + i,1,10); g.drawString ("Value of j in uuu " + j,1,40); } }
import java.awt.*; import java.applet.*; class uuu extends Thread { zzz b; uuu(zzz a) { b = a; } public void run() { int k = 0; while (k < 1000) { b.j++; b.repaint(); k++; } b.bb(); b.aa(); while (k < 2000) { b.j++; b.repaint(); k++; } b.bb(); } } class ttt extends Thread { zzz p; ttt(zzz q) { p = q; } public void run() { int l = 0; p.aa(); while (l < 1000) { p.i++; p.repaint(); l++; } p.bb(); p.aa(); while (l < 2000) { p.i++; p.repaint(); l++; } } } public class zzz extends Applet { ttt t; int i = 0; uuu u; int j=0; public synchronized void bb() { notify(); } public synchronized void aa() { try wait(); catch ( Exception e) {} } public void init() { t = new ttt(this); t.start(); u = new uuu(this); u.start(); } public boolean mouseDown(java.awt.Event evt, int x, int y) { return true; } public void paint(Graphics g) { g.drawString ("Value of i in ttt " + i,1,10); g.drawString ("Value of j in uuu " + j,1,40); } }
Hustle up to the next topic "At Work Then" or in the vein we've had in threads above (communication) notify us about your thoughts on this page; we'll be waiting.