What's deadlock in programming? [duplicate] - java

This question already has answers here:
What is a deadlock?
(19 answers)
Closed 9 years ago.
What is deadlock in programming Object Oriented ?
I had knew deadlock in transaction of Database Systems. But in programming I'm not clear.
I want to know when deadlock occur and how to resolve it.
Thanks!

A deadlock is when you have two or more processes that are each waiting for the other to finish. When this happens, neither one can continue and the program essentially stalls.

There is a basic example here
http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
well documented.
but, a deadlock occurs when you wait for a object to be freed but that condition is never achieved.

Related

stop(getSelf()) vs stop(this.getSelf()) [duplicate]

This question already has answers here:
In Java, what is the difference between this.method() and method()?
(9 answers)
Closed 4 years ago.
I have been studying Akka Actors. I know the meaning of stop(ActorRef) which kills the running actor. But what is the difference between getSelf() and this.getSelf() while killing an actor? Thank You in advance!.
There's no difference between them, it's just that some people believe it enhances readability.
Readability is an important thing in programming but in this specific case, I personally believe they are just as readable with or without the this prefix.

Pinging many devices simultaneously as a nonblocking thread in Java [duplicate]

This question already has answers here:
Ping multiple servers in Java [duplicate]
(2 answers)
How to ping an IP address
(16 answers)
Closed 5 years ago.
I want to ping many machines to obtain information of their reachability. Time is critical here and I dont want to wait 1-2 seconds for every machine (assuming that there are hundreds of them). I tried this code snippet and turns out that the isReachable method is synchronous.
InetAddress.getByName(host).isReachable(timeout);
I need a way to make either that method (if possible) nonblocking or come up with another solution that will help cut down the waiting time.
Any help would be greatly appreciated. Thanks in advance.

Should we avoid using wait/notify/notifyAll explicitly [duplicate]

This question already has answers here:
Is using Object.wait and Object.notify directly a code smell?
(2 answers)
Closed 7 years ago.
My question is about using wait() and notify() explicitly. Whould we try to avoid it every time we need something like producer-consumer and consider to use BlockingDeque instead.
I tried to think about it and didn't find more or less reasonble explanation. Moreover I tried to invent example where we would have to use wait() and notify() explicitly and failed.
There are better higher level constructs in java.util.concurrent, so using wait/notify nowadays is highly dubious.

Meaning and hazard of spurious wakeups in Java? [duplicate]

This question already has answers here:
Do spurious wakeups in Java actually happen?
(7 answers)
Closed 8 years ago.
What is the meaning of spurious wakeups in Java? Why they are so dangerous? Can you explain it with an example?
The JVM is allowed to wake a waiting thread without another thread calling its notify() method - a so called "spurious wakeup".
If you don't consider this possibility, you may proceed with processing when the wait state has not been achieved, leading to incorrect behaviour.
The correct approach when woken up is to first check that the state your thread is waiting on has actually been achieved, otherwise return to waiting by immediately calling wait()

Thread safe OutputStreamWriter [duplicate]

This question already has answers here:
Write to FileOutputStream from multiple threads in Java
(4 answers)
Closed 5 years ago.
Is there any thread-safe substitute for java.io.OutputStreamWriter in the JDK or some third party library?
None that I know of.
But you can use other means to effectively achieve thread-safety, like protecting the OutputStreamWriter with some monitor, Lock, or Semaphore. Also, you can use a single-threaded ExecutorService as a unique bottleneck through which other threads submit writing "jobs".
I answered this question: Write to FileOutputStream from multiple threads in Java
, which is exactly the same.
The short answer is no, but there are ways around it.

Categories

Resources