Advertisement
Guest User

Untitled

a guest
Jan 6th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. package console;
  2.  
  3. class Useless {
  4.  
  5. public static boolean b = true;
  6.  
  7. public synchronized void u1(){
  8. while(b == true){
  9. try {
  10. wait();
  11. }catch(InterruptedException i) {
  12.  
  13. }
  14. }
  15.  
  16. }
  17.  
  18.  
  19.  
  20. public synchronized void u2() {
  21. if (b == true) {
  22. b = false;
  23. }
  24. notify();
  25. }
  26. }
  27.  
  28.  
  29. public class SleepMessages extends Thread {
  30.  
  31. private Useless u;
  32. private long time;
  33.  
  34. public SleepMessages(Useless u, long time) {
  35. this.u=u;
  36. this.time=time;
  37. }
  38. public void run() {
  39. String importantInfo[] = {
  40. "Mares eat oats",
  41. "Does eat oats" };
  42.  
  43. for (int i = 0; i < importantInfo.length; i++) {
  44.  
  45. u.u1();
  46.  
  47. System.out.println(importantInfo[i] + " - " +
  48. getName() + " " + (System.currentTimeMillis()-time) + "ms");
  49. try {
  50. sleep(2000);
  51. } catch(InterruptedException e) {}
  52. }
  53. }
  54. public static void main(String args[]) throws
  55. InterruptedException {
  56.  
  57. long time = System.currentTimeMillis();
  58.  
  59. Useless u = new Useless();
  60. Thread t1 = new SleepMessages(u, time);
  61. t1.setName("t1");
  62. Thread t2 = new SleepMessages(u, time);
  63. t2.setName("t2");
  64. t1.start(); t2.start();
  65. sleep(2000);
  66. System.out.println("Here they go!..." + (System.currentTimeMillis() - time) + "ms");
  67. t1.interrupt();
  68. sleep(1000);
  69. t2.interrupt();
  70. u.u2();
  71. sleep(1000);
  72. u.u2();
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement