Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package console;
- class Useless {
- public static boolean b = true;
- public synchronized void u1(){
- while(b == true){
- try {
- wait();
- }catch(InterruptedException i) {
- }
- }
- }
- public synchronized void u2() {
- if (b == true) {
- b = false;
- }
- notify();
- }
- }
- public class SleepMessages extends Thread {
- private Useless u;
- private long time;
- public SleepMessages(Useless u, long time) {
- this.u=u;
- this.time=time;
- }
- public void run() {
- String importantInfo[] = {
- "Mares eat oats",
- "Does eat oats" };
- for (int i = 0; i < importantInfo.length; i++) {
- u.u1();
- System.out.println(importantInfo[i] + " - " +
- getName() + " " + (System.currentTimeMillis()-time) + "ms");
- try {
- sleep(2000);
- } catch(InterruptedException e) {}
- }
- }
- public static void main(String args[]) throws
- InterruptedException {
- long time = System.currentTimeMillis();
- Useless u = new Useless();
- Thread t1 = new SleepMessages(u, time);
- t1.setName("t1");
- Thread t2 = new SleepMessages(u, time);
- t2.setName("t2");
- t1.start(); t2.start();
- sleep(2000);
- System.out.println("Here they go!..." + (System.currentTimeMillis() - time) + "ms");
- t1.interrupt();
- sleep(1000);
- t2.interrupt();
- u.u2();
- sleep(1000);
- u.u2();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement