同步问题。应用程序冻结

时间:2019-02-19 03:48:35

标签: java android multithreading synchronized notify

我在同步对象方面遇到麻烦,需要您的帮助。我正在创建一个游戏,但我无法使同步线程正常工作。我正在尝试创建2个线程,它们每次更改textView时都会相互通知。您能帮我吗?预先谢谢您。这是我的代码:

   public void doPattern(int i) {

    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (lock1) {
                    try {
                        lock1.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tv.setText(text.get(0));
            synchronized (lock2) {
                lock2.notify();

            }
        }
    });
    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (lock2) {
                    try {
                        lock2.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tv.setText(text.get(1));

            synchronized (lock1) {
                lock1.notify();

            }
        }
    });
    t1.start();
    t2.start();
    try {
        t1.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    try {
        t2.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:0)

在这里,我放置了一个检查(布尔标志),而不是使用两个不同的锁来使用一个锁,这样t1总是总是首先设置文本。我已经使用条件while(1 == 1)来按无限顺序设置wait()和notifiy(),您可以停止自己的条件。我希望这会有所帮助。

volatile boolean flag=false;
public void doPattern(int i) {

    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
          synchronized (lock) 
          {

              flag =true;
              while(1==1)
               {   
                 try {
                      sleep(1000);
                     } catch (InterruptedException e) {
                      e.printStackTrace();
                     } 

                   tv.setText(text.get(0));
                   lock.notify();

                  try{ 
                    lock.wait();
                     }catch (InterruptedException e) {
                     e.printStackTrace();
                     }
                }
         }
    }});

    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (lock) {
                 if(!flag)
                  {
                    try {
                        lock.wait();
                        } catch (InterruptedException e) {
                          e.printStackTrace();
                        }
                  }     
            while(1==1)
               {
                     try {
                        sleep(1000);
                         } catch (InterruptedException e) {
                           e.printStackTrace();
                         }
                     tv.setText(text.get(1));
                     lock.notify();
                     try {
                        lock.wait();
                        } catch (InterruptedException e) {
                          e.printStackTrace();
                        }
           }
        }
    }});

    t1.start();
    t2.start();

    try {
        t1.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    try {
        t2.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}