Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help needed- Class with only one thread  RSS feed

 
Anand Natraj
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a specific requirement to create a class with only thread. That is i don't want any other class to create a thread in that class. Please help out.

Thanks in advance.

Regards,
Anand
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not following you, exactly. You need to create a class whose methods can only be called from a certain thread? You need to create a class that can only be constructed on a certain thread? You need to create a class whose methods can only be called from one thread at a time?

Can you explain your requirement more clearly? All of these things are quite possible -- you just need to explain which it is.
 
Anand Natraj
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,

Thanks for the reply.

Here is my requirment.

I want to create a class and make sure that at run time it always runs in a single thread and no one can create multiple threads on that class.

Thanks in advance for your solution.

Regards,
Anand
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, one obvious way is like this



That requires special code in every method, but it's simple and easy to implement, and I'm not sure I can come up with anything better.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!