Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

sharing data in threads  RSS feed

 
Ayub ali khan
Ranch Hand
Posts: 395
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have few questions about Threads, I appreciate your valuable answers.

How can two threads share data?

Suppose I have the following class:

class Abc{

synchronized mthod1(){

}

synchronized mthod2(){

}

}

If thread t1 is accessing mthod1, can thread t2 access mthod2()? (Am I correct that this is not possible?)

Thanks

Ayub
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how your two questions are related, but I can answer them both, anyway.

First, two threads can share data simply by holding references to the same object. They can both manipulate the data in that one object.

Second, two threads can't call those two methods on the same instance of Abc at the same time. If you have two different Abc instances, then one thread can be calling one method while another thread calls the other.
 
Ayub ali khan
Ranch Hand
Posts: 395
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,

Thank you very much for your valuaable answer. The questions were not related to one another.

Thank you !!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ayub ali khan:

How can two threads share data?

Demo:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!