• Post Reply Bookmark Topic Watch Topic
  • New Topic

reguarding multithreading  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 4 classes, Main, ClassA extends thread, ClassB extends thread, SomeLinkedList implements Runnable.
Question is, am i able to have class a and b thread, run on the same SomeLinkedList object? SomeLinkedList object has
public synchrnoized void add(), which ClassA uses,public synchrnoized void remove() which ClassB uses. is this possible? if so, how?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Willie Tsang wrote:I have 4 classes, Main, ClassA extends thread, ClassB extends thread, SomeLinkedList implements Runnable.


You shouldn't extend Thread, as you're not creating a specialized type of Thread. You're just creating a task to be run in its own thread, so you should implement Runnable.

Question is, am i able to have class a and b thread, run on the same SomeLinkedList object? SomeLinkedList object has
public synchrnoized void add(), which ClassA uses,public synchrnoized void remove() which ClassB uses. is this possible? if so, how?


It doesn't matter that ClassA and ClassB extend thread. Doesn't matter what class they are or if they're the same class. Doesn't matter if you're using one of them or more. All that matters is that if you have one or more synchronized methods on your SLL class, then, when a given thread of execution (lower case "t", nothing to do with Thread or any other class) is executing any one of those synced methods on one particular SLL object, no other thread can execute any other synced method on that same SLL object.

More generally: While any one thread holds a particular lock, no other thread may obtain that lock. That's all that syncing does in terms of mutual exclusion--obtains an object's lock, waiting until it's released if some other thread currently holds it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!