• Post Reply Bookmark Topic Watch Topic
  • New Topic

Relationship between user and main thread!

 
purushottam singh
Greenhorn
Posts: 10
MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain me, the relationship between user and main thread, except user thread spawned from main thread.
Suppose i have a main thread which spawned two user threads t1 and t2 . I have an ArrayList within main and i want to adds some value into it from thread t1 and then i want to read these values from thread t2. How can i implement above statement into codes??


Thanks in advance.
 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should do two things.

1. Make your instance of ArrayList visible to both threads;
2. Properly synchronize work with this list inside threads.

Please look at the sample code below (for simplicity i made some assumptions)
 
purushottam singh
Greenhorn
Posts: 10
MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrey..
 
nitin sethi
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrey Kozhanov wrote:You should do two things.

1. Make your instance of ArrayList visible to both threads;
2. Properly synchronize work with this list inside threads.

Please look at the sample code below (for simplicity i made some assumptions)


Hi Ranchers,

Can we code the situation as below too? I was wondering if there's any pros or cons of the below solution compared to the solution given by Andrey.



Regards,
Nitin Sethi
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!