Java - 不在多线程程序中循环

标签 java multithreading

我仍然是一个 Java 新手,正在尝试学习线程。我的问题是它不会循环 5 次。它运行一次并退出。我正在使用 a.class 锁定类对象,这样两个线程都锁定在同一个对象监视器上。

class a implements Runnable {
  Thread thr;
  int count;
  String time;

  a(String s) {
    thr = new Thread(this, s);
    thr.start();
  }

  public void run() {
    count++;

    if (Thread.currentThread().getName().compareTo("one") == 0) {
      synchronized (a.class) {

        try {
          for (int i = 0; i < 5; i++) {
            System.out.println("Now running thread " + Thread.currentThread().getName() + " with count " + count);

            time = "Tick";
            System.out.println(time);
            notify();

            while (time == "Tock") {
              wait();
            }
          }
        } catch (Exception e) {
        }

      }
    } else if (Thread.currentThread().getName().compareTo("two") == 0) {
      synchronized (a.class) {
        try {
          for (int j = 0; j < 5; j++) {
            System.out.println("Now running thread " + Thread.currentThread().getName() + " with count " + count);

            time = "Tock";
            System.out.println(time);
              notify();

            while (time == "Tick") {
              wait();
            }
          }
        } catch (Exception e) {
        }
      }
    }
  }
}

public class b {
  public static void main(String args[]) {

    a obj1 = new a("one");
    a obj2 = new a("two");
  }
}

最佳答案

给你,原始代码:

class a implements Runnable {
    Thread thr;
    int count;
    static String time = "Tock";

    a(String s) {
        thr = new Thread(this, s);
        thr.start();
    }

    public void run() {
        count++;

        if (Thread.currentThread().getName().compareTo("one") == 0) {
            synchronized (a.class) {

                try {
                    for (int i = 0; i < 5; i++) {
                        while (time.equals("Tock")) {
                            a.class.wait();
                        }

                        System.out.println("Now running thread "
                                + Thread.currentThread().getName()
                                + " with count " + count);

                        time = "Tock";
                        System.out.println(time);
                        a.class.notify();                       
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        } else if (Thread.currentThread().getName().compareTo("two") == 0) {
            synchronized (a.class) {
                try {
                    for (int j = 0; j < 5; j++) {
                        while (time.equals("Tick")) {
                            a.class.wait();
                        }

                        System.out.println("Now running thread "
                                + Thread.currentThread().getName()
                                + " with count " + count);

                        time = "Tick";
                        System.out.println(time);
                        a.class.notify();                       
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class Test {
    public static void main(String args[]) {

        a obj1 = new a("one");
        a obj2 = new a("two");
    }
}

问题是你在隐式 this 对象上调用 waitnotify ,而 >a.class 对象,因此您必须在 a.class 上调用 wait/notify。就是这样。

我还做了一个小的重组,因为我假设您希望它们以交替顺序打印 TickTock,对吗?

关于Java - 不在多线程程序中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379253/

相关文章:

java - 增加 JTextArea 上点指示器的大小

java - .action 扩展...它是什么?

java - 如何将 Java 8 设置为 Mac 上的默认设置?

java - 以线程安全的方式从数组列表返回对象?

c - POSIX 线程和公平性(信号量)

c# - 暂停方法设置 # of milliseconds

c++ - 我们是否需要将 volatile 关键字和互斥锁一起用于线程安全?

java - 如何远程部署war文件到远程glassfish?

WPF ICollectionView 刷新

java - 使用 ContentValues 将以零开头的字符串编号插入字符串列,但始终删除前导零