Я создал класс под названием Customer there, у меня есть два метода waitThread и notifyThread. Оба метода будут иметь один и тот же замок obj. waitThread в цикле while синхронизированного блока будет вызывать метод wait, блокировка thread1 будет выпущена, а другой поток сказать, что thread2 нужно пойти для метода notifyThread и установить значение флага true. Но выход iam получаетсяуведомление не вызывается только ожидание вызвано
И thread1, и thread2 входят в метод waitThread. Ни один из них не вводит метод notifyThread. Кто-нибудь может предложить, что здесь происходит, почему он не вводит метод notifyThread. Если ему нужно ввести notifyThread, что можно сделать?
public class Customer implements Runnable
{
public boolean flag=false;
public Object obj=new Object();
public void run()
{
waitThread();
notifyThread();
}
public void waitThread()
{
synchronized(obj)
{
System.out.println(Thread.currentThred().getName()+"in the waitThread");
while(!flag)
{
System.out.println(Thread.currentThred().getName()+"in the waitThread calling wait");
try
{
obj.wait();
}
catch(Exception e)
{
}
}
}
}
public void notifyThread()
{
synchronized(obj)
{
System.out.println(Thread.currentThred().getName()+"in the notifyThread");
flag=true;
System.out.println(Thread.currentThred().getName()+" notify the previous thread");
obj.notify();
}
}
}
}
Создание двух потоков
public class Test
{
public static void main(String args[])
{
Customer cus=new Customer();
Thread t1=new Thread(cus);
Thread t2=new Thread(cus);
t1.start();
t2.start();
}
}
Я не уверен, как вы скомпилировали свой код, орфографическую ошибку в 'Thread.currentThred()'. –