All Questions

1645
votes
39answers
557001 views

"implements Runnable" vs. "extends Thread"

From what time I've spent with threads in Java, I've found these two ways to write threads: With implements Runnable: public class MyRunnable implements Runnable { public void run() { //...
912
votes
33answers
714034 views

Difference between wait() and sleep()

What is the difference between a wait() and sleep() in Threads? Is my understanding that a wait()-ing Thread is still in running mode and uses CPU cycles but a sleep()-ing does not consume any CPU cy...
119
votes
12answers
214479 views

How to use wait and notify in Java?

I have 2 matrices and I need to multiply them and then print the results of each cell. As soon as one cell is ready I need to print it, but for example I need to print the [0][0] cell before cell [2][...
72
votes
9answers
33603 views

If i synchronized two methods on the same class, can they run simultaneously?

If i synchronized two methods on the same class, can they run simultaneously on the same object? for example: class A { public synchronized void methodA() { //method A } public s...
16
votes
2answers
18843 views

Why Thread.sleep is bad to use

Apologies for this repeated question but I haven't found any satisfactory answers yet. Most of the question had their own specific use case: Java - alternative to thread.sleep Is there any better or a...
1
votes
1answers
296 views

How notify method works

As per the javadoc notify Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary an...
5
votes
1answers
9385 views

synchronized object not locked by thread before notifyAll()

I want to have a boolean to notify some sections of the system that a specific service started. For some strange reason I'm getting the error java.lang.IllegalMonitorStateException: object not locked...
3
votes
3answers
2304 views

ExecutorService's shutdown() doesn't wait until all threads will be finished

I have a code where 4 threads run at the same time. I want to wait until all these 4 threads will be finished. And only after that to continue the app flow. I tried two approaches: Thread#join(), t...
0
votes
1answers
52 views

Parsing @Context UriInfo to java thread

I'm trying to parse @Context UriInfo to another thread and do some task. But when i try to run it, it gives the error as Exception in thread "Thread-691" org.jboss.resteasy.spi.LoggableFailure: Unab...
0
votes
1answers
860 views

Java: Waiting connection threads created by HTTP connection are alive for very long duration

I have a server side code which checks if SOAP service is up. Code looks like: String response = ""; while (response.length() == 0) { try { final URL url = new URL("DummySoapServiceURL")...

Previous Next