POSS Politeknik Aceh
Welcome to POSS POLITEKNIK ACEH
<<=======================>>
You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free.
POSS Politeknik Aceh
Welcome to POSS POLITEKNIK ACEH
<<=======================>>
You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free.
POSS Politeknik Aceh
Would you like to react to this message? Create an account in a few clicks or log in to continue.
POSS Politeknik Aceh

Melihat sifat keterbukaan Linus Torvalds dengan memerdekakan source kernel buatannya, membuat terdorong hati nurani kami untuk Go OPEN SOURCE.......
 
IndeksLatest imagesPencarianPendaftaranLogin
Similar topics
Latest topics
» Dasar - Dasar Python
What is deadlock in Java ? How to fix it ? I_icon_minitimeThu Mar 15, 2012 2:29 am by Admin

» Pemograman java... hello.java
What is deadlock in Java ? How to fix it ? I_icon_minitimeWed Mar 07, 2012 8:49 pm by zack

» Cracking WEP Menggunakan Backtrack
What is deadlock in Java ? How to fix it ? I_icon_minitimeWed Oct 12, 2011 9:07 am by zack

» sedikit tentang array
What is deadlock in Java ? How to fix it ? I_icon_minitimeWed Oct 12, 2011 8:54 am by zack

» Cara mengembalikan data yang telah terformat diubuntu
What is deadlock in Java ? How to fix it ? I_icon_minitimeWed Oct 12, 2011 8:19 am by zack

» Dasar-dasar PHP 12: Penutup
What is deadlock in Java ? How to fix it ? I_icon_minitimeTue Jul 12, 2011 10:31 am by Admin

» Dasar-dasar PHP 11: Menguasai Fungsi (bag. 2)
What is deadlock in Java ? How to fix it ? I_icon_minitimeTue Jul 12, 2011 10:30 am by Admin

» Dasar-dasar PHP 11: Menguasai Fungsi (bag 1)
What is deadlock in Java ? How to fix it ? I_icon_minitimeTue Jul 12, 2011 10:26 am by Admin

» Dasar-dasar PHP 10 — Hula Loops
What is deadlock in Java ? How to fix it ? I_icon_minitimeTue Jul 12, 2011 10:20 am by Admin


 

 What is deadlock in Java ? How to fix it ?

Go down 
PengirimMessage
zack

zack


Jumlah posting : 67
Join date : 13.04.11

What is deadlock in Java ? How to fix it ? Empty
PostSubyek: What is deadlock in Java ? How to fix it ?   What is deadlock in Java ? How to fix it ? I_icon_minitimeThu Apr 14, 2011 2:20 am

questions starts with "What is deadlock ?"
answer is simple , when two or more threads waiting for each other to release lock and get stuck for infinite time , situation is called deadlock . it will only happen in case of multithreading.

How do you detect deadlock ?
though this could have many answers , my version is first I would look the code if I see nested synchronized block or calling one synchronized method from other or trying to get lock on different object then there is good chance of deadlock if developer is not very careful.

other way is to find it when you actually get locked while running the application , try to take thread dump , in linux you can do this by command "kill -3" , this will print status of all the thread in application log file and you can see which thread is locked on which object.

other way is to use jconsole , jconsole will show you exactly which threads are get locked and on which object.

once you answer this , they may ask you to write code which will result in deadlock ?
here is one of my version

public void method1(){
synchronized(String.class){
System.out.println("Aquired lock on String.class object");

synchronized (Integer.class) {
System.out.println("Aquired lock on Integer.class object");
}
}
}

public void method2(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}

If method1() and method2() both will be called by two or many threads , there is a good chance of deadlock becuase if thead 1 aquires lock on Sting object while executing method1() and thread 2 aquires lock on Integer object while executing method2() both will be waiting for each other to release lock on Integer and String to proceed further which will never happen.

now interviewer comes to final part , one of the most important in my view , How to fix deadlock ?

if you have looked above code carefully you may have figured out that real reason for deadlock is not multiple threads but the way they access lock , if you provide an ordered access then problem will be resolved , here is
the fixed version.

public void method1(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}

public void method2(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}

Now there would not be any deadlock because both method is accessing lock on Integer and String object in same order . so if thead A aquires lock on Integer object , thread B will not proceed until thread A releases Integer lock , same way thread A will not be blocked even if thread B holds String lock because now thread B will not expect thread A to release Integer lock to proceed further.
Kembali Ke Atas Go down
 
What is deadlock in Java ? How to fix it ?
Kembali Ke Atas 
Halaman 1 dari 1
 Similar topics
-
» Pemograman java... hello.java

Permissions in this forum:Anda tidak dapat menjawab topik
POSS Politeknik Aceh :: PEMOGRAMAN :: JAVA-
Navigasi: