Thread isn't resuming execution after notify()












2














I have two classes (Customer and Till). Customer thread waits until it is notified by a till thread. In my program, the customer thread is not executing it's code after being notified by the till thread. The till thread continues it's execution.



Customer.java (Customer thread extends Thread)



import java.util.concurrent.*;
import java.util.*;

public class Customer extends Thread
{
Random random_generator = new Random();

public int minimumQueueLength;
public Set set;
public Iterator iterator;
public boolean placed_in_queue;

public List<Integer> queue_length_list;
public CopyOnWriteArrayList till_set = new CopyOnWriteArrayList();
public Till till, till_to_join;
public final Object lock;

public Customer(CopyOnWriteArrayList till_set)
{
this.till_set = till_set;
this.placed_in_queue = false;
queue_length_list = new ArrayList<Integer>();
lock = new Object();
}

public void run()
{
try
{
place_in_queue();
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}

if(placed_in_queue)
{
synchronized(this.lock)
{

System.out.println(this.getName()+" waiting");

try {
this.lock.wait();

System.out.println(this.getName()+" has been woken");

} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else
{
}
}

public void place_in_queue() throws InterruptedException
{
placed_in_queue = false;
iterator = till_set.iterator();

while(iterator.hasNext())
{
till = (Till)iterator.next();
queue_length_list.add(till.customer_queue.size());
}

minimumQueueLength =
queue_length_list.indexOf(Collections.min(queue_length_list));

if(minimumQueueLength < 5)
{
try
{
till_to_join = (Till)till_set.get(minimumQueueLength);
till_to_join.customer_queue.put(this);
placed_in_queue = true;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


Till.java (till thread extends Thread)



import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.*;

public class Till extends Thread
{
BlockingQueue<String> item_queue = new ArrayBlockingQueue<String>(200);
BlockingQueue<Customer> customer_queue = new ArrayBlockingQueue<Customer>(10);

public Random random;
public Customer c;

public Till(BlockingQueue<String> item_queue) throws InterruptedException
{
this.item_queue = item_queue;
random = new Random();
}

public void run()
{
while(true)
{
try
{
c = customer_queue.take();

synchronized(c.lock)
{
System.out.println(this.getName()+" Waking up : "+c.getName());
c.lock.notify();
System.out.println(c.getName()+" has been notified!");
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}


CustomerGenerator.java



import java.util.*;
import java.util.concurrent.*;

public class CustomerGenerator extends Thread
{
public int customer_generation_rate;

//0 - slow
//1 - fast

public Random random_generator;

public static BlockingQueue<String> item_queue = new ArrayBlockingQueue<String>(200);
public static CopyOnWriteArrayList till_set = new CopyOnWriteArrayList();

public int i;

public CustomerGenerator(int customer_generation_rate, CopyOnWriteArrayList till_set)
{
this.customer_generation_rate = customer_generation_rate;
this.till_set = till_set;
this.i = 0;
random_generator = new Random();
}

public void run()
{
while(i<1)
{
switch(customer_generation_rate)
{
case 0 : try
{
Thread.sleep(random_generator.nextInt(1000));
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

case 1 : try
{
Thread.sleep(random_generator.nextInt(500));
}
catch(InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

default : customer_generation_rate = 0;
break;
}

Customer customer = new Customer(till_set);
customer.start();
total_customer_count++;
i++;
}
}
}


Driver.java



import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Driver
{
public static BlockingQueue<String> item_queue = new ArrayBlockingQueue<>(200);

public static CopyOnWriteArrayList<Till> till_set = new CopyOnWriteArrayList<Till>();

public static Set set;
public static Iterator iterator;

public static int i;

public static final int till_count = 5;

public static Thread till_thread;

public static Till till_object;

public static ExecutorService till_service = Executors.newFixedThreadPool(5);

public static void main(final String args) throws InterruptedException
{

for(i=0; i<till_count; i++)
{
till_object = new Till(item_queue);
till_set.add(till_object);
}

final CustomerGenerator customer_generator = new CustomerGenerator(0, till_set);
customer_generator.start();

Thread.sleep(5000);

for(final Till t : till_set)
{
till_service.submit(t);
}
}
}


Output Obtained:



Thread-7 waiting

Thread-1 Waking up : Thread-7

Thread-7 has been notified!



Expected Output:



Thread-7 waiting

Thread-1 Waking up : Thread-7

Thread-7 has been notified!

Thread-7 has been woken



Please help. Thank you. :)










share|improve this question
























  • Please format your code. And please include a full Minimal, Complete, and Verifiable example - you're probably just not getting the thread that is waiting from your queue, but you haven't included the code that sets up your threads. And also, as mentioned in the Javadoc of Thread - you shouldn't call wait/notify on a Thread object, as the Thread object already uses these for its own purposes. ("It is recommended that applications not use wait, notify, or notifyAll on Thread instances.")
    – Erwin Bolwidt
    Nov 13 '18 at 0:56










  • Using the wait and notify methods of Thread objects is not recommended, as it interferes with their operation. Consider making Customer implement Runnable without extending anything, then creating threads with new Thread(customer).
    – VGR
    Nov 13 '18 at 0:58












  • Hi @ErwinBolwidt, I've added the minimal and complete code that my issue can be reproduced :)
    – james
    Nov 13 '18 at 1:33










  • Hi @VGR .. I modified the code and tried using a lock object instead but I'm still facing the same issue :(
    – james
    Nov 13 '18 at 1:37
















2














I have two classes (Customer and Till). Customer thread waits until it is notified by a till thread. In my program, the customer thread is not executing it's code after being notified by the till thread. The till thread continues it's execution.



Customer.java (Customer thread extends Thread)



import java.util.concurrent.*;
import java.util.*;

public class Customer extends Thread
{
Random random_generator = new Random();

public int minimumQueueLength;
public Set set;
public Iterator iterator;
public boolean placed_in_queue;

public List<Integer> queue_length_list;
public CopyOnWriteArrayList till_set = new CopyOnWriteArrayList();
public Till till, till_to_join;
public final Object lock;

public Customer(CopyOnWriteArrayList till_set)
{
this.till_set = till_set;
this.placed_in_queue = false;
queue_length_list = new ArrayList<Integer>();
lock = new Object();
}

public void run()
{
try
{
place_in_queue();
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}

if(placed_in_queue)
{
synchronized(this.lock)
{

System.out.println(this.getName()+" waiting");

try {
this.lock.wait();

System.out.println(this.getName()+" has been woken");

} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else
{
}
}

public void place_in_queue() throws InterruptedException
{
placed_in_queue = false;
iterator = till_set.iterator();

while(iterator.hasNext())
{
till = (Till)iterator.next();
queue_length_list.add(till.customer_queue.size());
}

minimumQueueLength =
queue_length_list.indexOf(Collections.min(queue_length_list));

if(minimumQueueLength < 5)
{
try
{
till_to_join = (Till)till_set.get(minimumQueueLength);
till_to_join.customer_queue.put(this);
placed_in_queue = true;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


Till.java (till thread extends Thread)



import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.*;

public class Till extends Thread
{
BlockingQueue<String> item_queue = new ArrayBlockingQueue<String>(200);
BlockingQueue<Customer> customer_queue = new ArrayBlockingQueue<Customer>(10);

public Random random;
public Customer c;

public Till(BlockingQueue<String> item_queue) throws InterruptedException
{
this.item_queue = item_queue;
random = new Random();
}

public void run()
{
while(true)
{
try
{
c = customer_queue.take();

synchronized(c.lock)
{
System.out.println(this.getName()+" Waking up : "+c.getName());
c.lock.notify();
System.out.println(c.getName()+" has been notified!");
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}


CustomerGenerator.java



import java.util.*;
import java.util.concurrent.*;

public class CustomerGenerator extends Thread
{
public int customer_generation_rate;

//0 - slow
//1 - fast

public Random random_generator;

public static BlockingQueue<String> item_queue = new ArrayBlockingQueue<String>(200);
public static CopyOnWriteArrayList till_set = new CopyOnWriteArrayList();

public int i;

public CustomerGenerator(int customer_generation_rate, CopyOnWriteArrayList till_set)
{
this.customer_generation_rate = customer_generation_rate;
this.till_set = till_set;
this.i = 0;
random_generator = new Random();
}

public void run()
{
while(i<1)
{
switch(customer_generation_rate)
{
case 0 : try
{
Thread.sleep(random_generator.nextInt(1000));
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

case 1 : try
{
Thread.sleep(random_generator.nextInt(500));
}
catch(InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

default : customer_generation_rate = 0;
break;
}

Customer customer = new Customer(till_set);
customer.start();
total_customer_count++;
i++;
}
}
}


Driver.java



import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Driver
{
public static BlockingQueue<String> item_queue = new ArrayBlockingQueue<>(200);

public static CopyOnWriteArrayList<Till> till_set = new CopyOnWriteArrayList<Till>();

public static Set set;
public static Iterator iterator;

public static int i;

public static final int till_count = 5;

public static Thread till_thread;

public static Till till_object;

public static ExecutorService till_service = Executors.newFixedThreadPool(5);

public static void main(final String args) throws InterruptedException
{

for(i=0; i<till_count; i++)
{
till_object = new Till(item_queue);
till_set.add(till_object);
}

final CustomerGenerator customer_generator = new CustomerGenerator(0, till_set);
customer_generator.start();

Thread.sleep(5000);

for(final Till t : till_set)
{
till_service.submit(t);
}
}
}


Output Obtained:



Thread-7 waiting

Thread-1 Waking up : Thread-7

Thread-7 has been notified!



Expected Output:



Thread-7 waiting

Thread-1 Waking up : Thread-7

Thread-7 has been notified!

Thread-7 has been woken



Please help. Thank you. :)










share|improve this question
























  • Please format your code. And please include a full Minimal, Complete, and Verifiable example - you're probably just not getting the thread that is waiting from your queue, but you haven't included the code that sets up your threads. And also, as mentioned in the Javadoc of Thread - you shouldn't call wait/notify on a Thread object, as the Thread object already uses these for its own purposes. ("It is recommended that applications not use wait, notify, or notifyAll on Thread instances.")
    – Erwin Bolwidt
    Nov 13 '18 at 0:56










  • Using the wait and notify methods of Thread objects is not recommended, as it interferes with their operation. Consider making Customer implement Runnable without extending anything, then creating threads with new Thread(customer).
    – VGR
    Nov 13 '18 at 0:58












  • Hi @ErwinBolwidt, I've added the minimal and complete code that my issue can be reproduced :)
    – james
    Nov 13 '18 at 1:33










  • Hi @VGR .. I modified the code and tried using a lock object instead but I'm still facing the same issue :(
    – james
    Nov 13 '18 at 1:37














2












2








2







I have two classes (Customer and Till). Customer thread waits until it is notified by a till thread. In my program, the customer thread is not executing it's code after being notified by the till thread. The till thread continues it's execution.



Customer.java (Customer thread extends Thread)



import java.util.concurrent.*;
import java.util.*;

public class Customer extends Thread
{
Random random_generator = new Random();

public int minimumQueueLength;
public Set set;
public Iterator iterator;
public boolean placed_in_queue;

public List<Integer> queue_length_list;
public CopyOnWriteArrayList till_set = new CopyOnWriteArrayList();
public Till till, till_to_join;
public final Object lock;

public Customer(CopyOnWriteArrayList till_set)
{
this.till_set = till_set;
this.placed_in_queue = false;
queue_length_list = new ArrayList<Integer>();
lock = new Object();
}

public void run()
{
try
{
place_in_queue();
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}

if(placed_in_queue)
{
synchronized(this.lock)
{

System.out.println(this.getName()+" waiting");

try {
this.lock.wait();

System.out.println(this.getName()+" has been woken");

} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else
{
}
}

public void place_in_queue() throws InterruptedException
{
placed_in_queue = false;
iterator = till_set.iterator();

while(iterator.hasNext())
{
till = (Till)iterator.next();
queue_length_list.add(till.customer_queue.size());
}

minimumQueueLength =
queue_length_list.indexOf(Collections.min(queue_length_list));

if(minimumQueueLength < 5)
{
try
{
till_to_join = (Till)till_set.get(minimumQueueLength);
till_to_join.customer_queue.put(this);
placed_in_queue = true;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


Till.java (till thread extends Thread)



import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.*;

public class Till extends Thread
{
BlockingQueue<String> item_queue = new ArrayBlockingQueue<String>(200);
BlockingQueue<Customer> customer_queue = new ArrayBlockingQueue<Customer>(10);

public Random random;
public Customer c;

public Till(BlockingQueue<String> item_queue) throws InterruptedException
{
this.item_queue = item_queue;
random = new Random();
}

public void run()
{
while(true)
{
try
{
c = customer_queue.take();

synchronized(c.lock)
{
System.out.println(this.getName()+" Waking up : "+c.getName());
c.lock.notify();
System.out.println(c.getName()+" has been notified!");
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}


CustomerGenerator.java



import java.util.*;
import java.util.concurrent.*;

public class CustomerGenerator extends Thread
{
public int customer_generation_rate;

//0 - slow
//1 - fast

public Random random_generator;

public static BlockingQueue<String> item_queue = new ArrayBlockingQueue<String>(200);
public static CopyOnWriteArrayList till_set = new CopyOnWriteArrayList();

public int i;

public CustomerGenerator(int customer_generation_rate, CopyOnWriteArrayList till_set)
{
this.customer_generation_rate = customer_generation_rate;
this.till_set = till_set;
this.i = 0;
random_generator = new Random();
}

public void run()
{
while(i<1)
{
switch(customer_generation_rate)
{
case 0 : try
{
Thread.sleep(random_generator.nextInt(1000));
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

case 1 : try
{
Thread.sleep(random_generator.nextInt(500));
}
catch(InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

default : customer_generation_rate = 0;
break;
}

Customer customer = new Customer(till_set);
customer.start();
total_customer_count++;
i++;
}
}
}


Driver.java



import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Driver
{
public static BlockingQueue<String> item_queue = new ArrayBlockingQueue<>(200);

public static CopyOnWriteArrayList<Till> till_set = new CopyOnWriteArrayList<Till>();

public static Set set;
public static Iterator iterator;

public static int i;

public static final int till_count = 5;

public static Thread till_thread;

public static Till till_object;

public static ExecutorService till_service = Executors.newFixedThreadPool(5);

public static void main(final String args) throws InterruptedException
{

for(i=0; i<till_count; i++)
{
till_object = new Till(item_queue);
till_set.add(till_object);
}

final CustomerGenerator customer_generator = new CustomerGenerator(0, till_set);
customer_generator.start();

Thread.sleep(5000);

for(final Till t : till_set)
{
till_service.submit(t);
}
}
}


Output Obtained:



Thread-7 waiting

Thread-1 Waking up : Thread-7

Thread-7 has been notified!



Expected Output:



Thread-7 waiting

Thread-1 Waking up : Thread-7

Thread-7 has been notified!

Thread-7 has been woken



Please help. Thank you. :)










share|improve this question















I have two classes (Customer and Till). Customer thread waits until it is notified by a till thread. In my program, the customer thread is not executing it's code after being notified by the till thread. The till thread continues it's execution.



Customer.java (Customer thread extends Thread)



import java.util.concurrent.*;
import java.util.*;

public class Customer extends Thread
{
Random random_generator = new Random();

public int minimumQueueLength;
public Set set;
public Iterator iterator;
public boolean placed_in_queue;

public List<Integer> queue_length_list;
public CopyOnWriteArrayList till_set = new CopyOnWriteArrayList();
public Till till, till_to_join;
public final Object lock;

public Customer(CopyOnWriteArrayList till_set)
{
this.till_set = till_set;
this.placed_in_queue = false;
queue_length_list = new ArrayList<Integer>();
lock = new Object();
}

public void run()
{
try
{
place_in_queue();
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}

if(placed_in_queue)
{
synchronized(this.lock)
{

System.out.println(this.getName()+" waiting");

try {
this.lock.wait();

System.out.println(this.getName()+" has been woken");

} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else
{
}
}

public void place_in_queue() throws InterruptedException
{
placed_in_queue = false;
iterator = till_set.iterator();

while(iterator.hasNext())
{
till = (Till)iterator.next();
queue_length_list.add(till.customer_queue.size());
}

minimumQueueLength =
queue_length_list.indexOf(Collections.min(queue_length_list));

if(minimumQueueLength < 5)
{
try
{
till_to_join = (Till)till_set.get(minimumQueueLength);
till_to_join.customer_queue.put(this);
placed_in_queue = true;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


Till.java (till thread extends Thread)



import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.*;

public class Till extends Thread
{
BlockingQueue<String> item_queue = new ArrayBlockingQueue<String>(200);
BlockingQueue<Customer> customer_queue = new ArrayBlockingQueue<Customer>(10);

public Random random;
public Customer c;

public Till(BlockingQueue<String> item_queue) throws InterruptedException
{
this.item_queue = item_queue;
random = new Random();
}

public void run()
{
while(true)
{
try
{
c = customer_queue.take();

synchronized(c.lock)
{
System.out.println(this.getName()+" Waking up : "+c.getName());
c.lock.notify();
System.out.println(c.getName()+" has been notified!");
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}


CustomerGenerator.java



import java.util.*;
import java.util.concurrent.*;

public class CustomerGenerator extends Thread
{
public int customer_generation_rate;

//0 - slow
//1 - fast

public Random random_generator;

public static BlockingQueue<String> item_queue = new ArrayBlockingQueue<String>(200);
public static CopyOnWriteArrayList till_set = new CopyOnWriteArrayList();

public int i;

public CustomerGenerator(int customer_generation_rate, CopyOnWriteArrayList till_set)
{
this.customer_generation_rate = customer_generation_rate;
this.till_set = till_set;
this.i = 0;
random_generator = new Random();
}

public void run()
{
while(i<1)
{
switch(customer_generation_rate)
{
case 0 : try
{
Thread.sleep(random_generator.nextInt(1000));
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

case 1 : try
{
Thread.sleep(random_generator.nextInt(500));
}
catch(InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;

default : customer_generation_rate = 0;
break;
}

Customer customer = new Customer(till_set);
customer.start();
total_customer_count++;
i++;
}
}
}


Driver.java



import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Driver
{
public static BlockingQueue<String> item_queue = new ArrayBlockingQueue<>(200);

public static CopyOnWriteArrayList<Till> till_set = new CopyOnWriteArrayList<Till>();

public static Set set;
public static Iterator iterator;

public static int i;

public static final int till_count = 5;

public static Thread till_thread;

public static Till till_object;

public static ExecutorService till_service = Executors.newFixedThreadPool(5);

public static void main(final String args) throws InterruptedException
{

for(i=0; i<till_count; i++)
{
till_object = new Till(item_queue);
till_set.add(till_object);
}

final CustomerGenerator customer_generator = new CustomerGenerator(0, till_set);
customer_generator.start();

Thread.sleep(5000);

for(final Till t : till_set)
{
till_service.submit(t);
}
}
}


Output Obtained:



Thread-7 waiting

Thread-1 Waking up : Thread-7

Thread-7 has been notified!



Expected Output:



Thread-7 waiting

Thread-1 Waking up : Thread-7

Thread-7 has been notified!

Thread-7 has been woken



Please help. Thank you. :)







java multithreading parallel-processing wait notify






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:47









c0der

8,21051744




8,21051744










asked Nov 13 '18 at 0:45









jamesjames

114




114












  • Please format your code. And please include a full Minimal, Complete, and Verifiable example - you're probably just not getting the thread that is waiting from your queue, but you haven't included the code that sets up your threads. And also, as mentioned in the Javadoc of Thread - you shouldn't call wait/notify on a Thread object, as the Thread object already uses these for its own purposes. ("It is recommended that applications not use wait, notify, or notifyAll on Thread instances.")
    – Erwin Bolwidt
    Nov 13 '18 at 0:56










  • Using the wait and notify methods of Thread objects is not recommended, as it interferes with their operation. Consider making Customer implement Runnable without extending anything, then creating threads with new Thread(customer).
    – VGR
    Nov 13 '18 at 0:58












  • Hi @ErwinBolwidt, I've added the minimal and complete code that my issue can be reproduced :)
    – james
    Nov 13 '18 at 1:33










  • Hi @VGR .. I modified the code and tried using a lock object instead but I'm still facing the same issue :(
    – james
    Nov 13 '18 at 1:37


















  • Please format your code. And please include a full Minimal, Complete, and Verifiable example - you're probably just not getting the thread that is waiting from your queue, but you haven't included the code that sets up your threads. And also, as mentioned in the Javadoc of Thread - you shouldn't call wait/notify on a Thread object, as the Thread object already uses these for its own purposes. ("It is recommended that applications not use wait, notify, or notifyAll on Thread instances.")
    – Erwin Bolwidt
    Nov 13 '18 at 0:56










  • Using the wait and notify methods of Thread objects is not recommended, as it interferes with their operation. Consider making Customer implement Runnable without extending anything, then creating threads with new Thread(customer).
    – VGR
    Nov 13 '18 at 0:58












  • Hi @ErwinBolwidt, I've added the minimal and complete code that my issue can be reproduced :)
    – james
    Nov 13 '18 at 1:33










  • Hi @VGR .. I modified the code and tried using a lock object instead but I'm still facing the same issue :(
    – james
    Nov 13 '18 at 1:37
















Please format your code. And please include a full Minimal, Complete, and Verifiable example - you're probably just not getting the thread that is waiting from your queue, but you haven't included the code that sets up your threads. And also, as mentioned in the Javadoc of Thread - you shouldn't call wait/notify on a Thread object, as the Thread object already uses these for its own purposes. ("It is recommended that applications not use wait, notify, or notifyAll on Thread instances.")
– Erwin Bolwidt
Nov 13 '18 at 0:56




Please format your code. And please include a full Minimal, Complete, and Verifiable example - you're probably just not getting the thread that is waiting from your queue, but you haven't included the code that sets up your threads. And also, as mentioned in the Javadoc of Thread - you shouldn't call wait/notify on a Thread object, as the Thread object already uses these for its own purposes. ("It is recommended that applications not use wait, notify, or notifyAll on Thread instances.")
– Erwin Bolwidt
Nov 13 '18 at 0:56












Using the wait and notify methods of Thread objects is not recommended, as it interferes with their operation. Consider making Customer implement Runnable without extending anything, then creating threads with new Thread(customer).
– VGR
Nov 13 '18 at 0:58






Using the wait and notify methods of Thread objects is not recommended, as it interferes with their operation. Consider making Customer implement Runnable without extending anything, then creating threads with new Thread(customer).
– VGR
Nov 13 '18 at 0:58














Hi @ErwinBolwidt, I've added the minimal and complete code that my issue can be reproduced :)
– james
Nov 13 '18 at 1:33




Hi @ErwinBolwidt, I've added the minimal and complete code that my issue can be reproduced :)
– james
Nov 13 '18 at 1:33












Hi @VGR .. I modified the code and tried using a lock object instead but I'm still facing the same issue :(
– james
Nov 13 '18 at 1:37




Hi @VGR .. I modified the code and tried using a lock object instead but I'm still facing the same issue :(
– james
Nov 13 '18 at 1:37












2 Answers
2






active

oldest

votes


















0














Just wait from the Till until it the Queue gets more than zero elements. From the customer thread after adding themself to the queue, notify the Till.






share|improve this answer





























    0














    CustomerGenerator generates one customer only when invoked. Making a mcve version of it makes it very clear:



    //i was initialized: i=0;
    public void run()
    {
    while(i<1)
    {
    final Customer customer = new Customer(till_set);
    customer.start();
    i++;
    }
    }


    I do not think that is what you meant.

    I find mcve a very useful technique. Not only it makes helping much easier, it
    is a powerful debugging tool. It many case, while preparing one, you are likely to find the problem. mcve should demonstrate the problem, and not your application.



    There may be other issues in the code. For more help please post Mcve.

    Some other comments:



    In CustomerGenerator you pass a reference of all tills to a Customer by:
    final Customer customer = new Customer(till_set); which is later used for selecting a till. I think till selection calculation would better be done in another class, say TillsManager which can have a stack of all customers waiting for a till.



    In Driver defining



     public static Till till_object; 
    for(i=0; i<5 ; i++)
    {
    till_object = new Till(item_queue);
    till_set.add(till_object);
    }


    means you will end up with 5 times the same object in till_set. I assume you wanted :



     for(i=0; i<till_count; i++)
    {
    Till till_object = new Till(item_queue);
    till_set.add(till_object);
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272176%2fthread-isnt-resuming-execution-after-notify%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Just wait from the Till until it the Queue gets more than zero elements. From the customer thread after adding themself to the queue, notify the Till.






      share|improve this answer


























        0














        Just wait from the Till until it the Queue gets more than zero elements. From the customer thread after adding themself to the queue, notify the Till.






        share|improve this answer
























          0












          0








          0






          Just wait from the Till until it the Queue gets more than zero elements. From the customer thread after adding themself to the queue, notify the Till.






          share|improve this answer












          Just wait from the Till until it the Queue gets more than zero elements. From the customer thread after adding themself to the queue, notify the Till.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 12:50









          Shamitha SilvaShamitha Silva

          1,31011022




          1,31011022

























              0














              CustomerGenerator generates one customer only when invoked. Making a mcve version of it makes it very clear:



              //i was initialized: i=0;
              public void run()
              {
              while(i<1)
              {
              final Customer customer = new Customer(till_set);
              customer.start();
              i++;
              }
              }


              I do not think that is what you meant.

              I find mcve a very useful technique. Not only it makes helping much easier, it
              is a powerful debugging tool. It many case, while preparing one, you are likely to find the problem. mcve should demonstrate the problem, and not your application.



              There may be other issues in the code. For more help please post Mcve.

              Some other comments:



              In CustomerGenerator you pass a reference of all tills to a Customer by:
              final Customer customer = new Customer(till_set); which is later used for selecting a till. I think till selection calculation would better be done in another class, say TillsManager which can have a stack of all customers waiting for a till.



              In Driver defining



               public static Till till_object; 
              for(i=0; i<5 ; i++)
              {
              till_object = new Till(item_queue);
              till_set.add(till_object);
              }


              means you will end up with 5 times the same object in till_set. I assume you wanted :



               for(i=0; i<till_count; i++)
              {
              Till till_object = new Till(item_queue);
              till_set.add(till_object);
              }





              share|improve this answer


























                0














                CustomerGenerator generates one customer only when invoked. Making a mcve version of it makes it very clear:



                //i was initialized: i=0;
                public void run()
                {
                while(i<1)
                {
                final Customer customer = new Customer(till_set);
                customer.start();
                i++;
                }
                }


                I do not think that is what you meant.

                I find mcve a very useful technique. Not only it makes helping much easier, it
                is a powerful debugging tool. It many case, while preparing one, you are likely to find the problem. mcve should demonstrate the problem, and not your application.



                There may be other issues in the code. For more help please post Mcve.

                Some other comments:



                In CustomerGenerator you pass a reference of all tills to a Customer by:
                final Customer customer = new Customer(till_set); which is later used for selecting a till. I think till selection calculation would better be done in another class, say TillsManager which can have a stack of all customers waiting for a till.



                In Driver defining



                 public static Till till_object; 
                for(i=0; i<5 ; i++)
                {
                till_object = new Till(item_queue);
                till_set.add(till_object);
                }


                means you will end up with 5 times the same object in till_set. I assume you wanted :



                 for(i=0; i<till_count; i++)
                {
                Till till_object = new Till(item_queue);
                till_set.add(till_object);
                }





                share|improve this answer
























                  0












                  0








                  0






                  CustomerGenerator generates one customer only when invoked. Making a mcve version of it makes it very clear:



                  //i was initialized: i=0;
                  public void run()
                  {
                  while(i<1)
                  {
                  final Customer customer = new Customer(till_set);
                  customer.start();
                  i++;
                  }
                  }


                  I do not think that is what you meant.

                  I find mcve a very useful technique. Not only it makes helping much easier, it
                  is a powerful debugging tool. It many case, while preparing one, you are likely to find the problem. mcve should demonstrate the problem, and not your application.



                  There may be other issues in the code. For more help please post Mcve.

                  Some other comments:



                  In CustomerGenerator you pass a reference of all tills to a Customer by:
                  final Customer customer = new Customer(till_set); which is later used for selecting a till. I think till selection calculation would better be done in another class, say TillsManager which can have a stack of all customers waiting for a till.



                  In Driver defining



                   public static Till till_object; 
                  for(i=0; i<5 ; i++)
                  {
                  till_object = new Till(item_queue);
                  till_set.add(till_object);
                  }


                  means you will end up with 5 times the same object in till_set. I assume you wanted :



                   for(i=0; i<till_count; i++)
                  {
                  Till till_object = new Till(item_queue);
                  till_set.add(till_object);
                  }





                  share|improve this answer












                  CustomerGenerator generates one customer only when invoked. Making a mcve version of it makes it very clear:



                  //i was initialized: i=0;
                  public void run()
                  {
                  while(i<1)
                  {
                  final Customer customer = new Customer(till_set);
                  customer.start();
                  i++;
                  }
                  }


                  I do not think that is what you meant.

                  I find mcve a very useful technique. Not only it makes helping much easier, it
                  is a powerful debugging tool. It many case, while preparing one, you are likely to find the problem. mcve should demonstrate the problem, and not your application.



                  There may be other issues in the code. For more help please post Mcve.

                  Some other comments:



                  In CustomerGenerator you pass a reference of all tills to a Customer by:
                  final Customer customer = new Customer(till_set); which is later used for selecting a till. I think till selection calculation would better be done in another class, say TillsManager which can have a stack of all customers waiting for a till.



                  In Driver defining



                   public static Till till_object; 
                  for(i=0; i<5 ; i++)
                  {
                  till_object = new Till(item_queue);
                  till_set.add(till_object);
                  }


                  means you will end up with 5 times the same object in till_set. I assume you wanted :



                   for(i=0; i<till_count; i++)
                  {
                  Till till_object = new Till(item_queue);
                  till_set.add(till_object);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 5:22









                  c0derc0der

                  8,21051744




                  8,21051744






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272176%2fthread-isnt-resuming-execution-after-notify%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()