Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to achieve the threadsafe without using the synchronization method or block?

Can an body explain alternate way to achieve the threadsafe without using the synchronization at method level block level?

public synchronized int disp()
 {

  }



 or
 public int  disp()
 {
  Synchronized
   {}
  }
like image 903
user1782621 Avatar asked Dec 29 '25 22:12

user1782621


1 Answers

You could just use a single thread, of course.

You can design your application to avoid sharing mutable state, so that methods are inherently threadsafe.

You can use synchronized library collections.

You can use threadsafe queues to communicate between threads (Concurrent Queues).

It rather depends on what you are trying to achieve...

like image 198
DNA Avatar answered Jan 01 '26 11:01

DNA