• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Q1) What are the thread-to-thread communication?
Q2)what is a deadlock? How can you avoid a deadlock?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit sanghai:

Q1) What are the thread-to-thread communication?
Q2)what is a deadlock? How can you avoid a deadlock?


Thread to thread communications can be achieved by local variables I mean the common data segment they both share.
a deadlock occurs when one thread is waiting on the resource
claimed by another thread and the ( another)second thread is waiting for the resource claimed by the first thread.
The best way to avoid is to use yield method so that for some time the resources claimed by the thread are released and other threads can run.
Sasidhar
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi sasi,
I did not understand your answer to the first question.

2nd question : I think that if you call yield(), then the scheduler can run the thread which called the yield() method if all the other threads are of the same or lower priority. So we cannot avoid deadlock using yield() method. Can we avoid deadlock using sleep() method or any other method?
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't use yeild() or sleep() to get yourself out of deadlock because neither of those methods releases locks which is what needs to be released when you have a deadlock. You need to use notify() or notifyAll() to prevent deadlocks.
Yield can work if your OS doesn't support time-slicing and your current thread is hanging.
Bill
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To Bill,
I think that yield() can work only when the scheduler does not support time-slicing and all the other threads are of higher priority. Am I true???
To Sasi,
I didnot understand your answer to the first question!!!
 
sasi dhulipala
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit sanghai:
To Sasi,
I didnot understand your answer to the first question!!![/B]


Hi
I have explained this in one my earlier postings...I am giving it again down below:
I would like to explain this with an example
/file: example.java
class example extends Thread
{
public increment localIncr;
example( increment var)
{
localIncr = var;
}
public static void main(String a[])
{
increment obj = new increment();
example one = new example(obj);
example two = new example(obj);
one.start();
two.start();
}
public void run()
{
localIncr.addone();
}
}

class increment
{
int incrVar;
public void addone()
{
incrVar++;
System.out.println("var Val : "+ incrVar);
}
}

Here in above the example class extends Thread class and creates two variables one and two of class exapmle in the main method. To the constructor of the example class I am passing a reference to
an increment object. you can see that while creating two thread one and two I am passing the same reference obj. Basically this increment object is common to both the thread objects one and two This is what is data sharing..
Multiple threads always share and operate in the same data segment. In other words they share the same process space.
Now the run method of example class tries to call addone method of increment object passed to it.. depending at what time there is a context switch between the threads ( I mean depending on when the OS decide to switch the threads) one and two the final value printed by addone method would vary.
I hope I am clear.
Sasi.
reply
    Bookmark Topic Watch Topic
  • New Topic