Threads
To be used in a method to a piece of code to make not thread safe classes in safe classes to be used by threads
public void synchonized add(number ){
// This method will be blocked to be used by one thread at the time
}
public void add(){
//
synchonized ( number ) {
// The code inside this block will be tread safe
// The code can inlcude synchonized methods
}
}
- ExecutorService
Creates a thread pool and wait until all the elements had completed his job
ExcecutorService es = Executors.newFixedThreadPool( number_of_threads_to_work_);
submit add and execute the thread
es.submit( thread_class );
Avoid to add more work to the ExecutorService
es.shutdown()
Avoid to add more work to the ExecutorService
es.shutdown()
- Synchonized
To be used in a method to a piece of code to make not thread safe classes in safe classes to be used by threads
public void synchonized add(number ){
// This method will be blocked to be used by one thread at the time
}
public void add(){
//
synchonized ( number ) {
// The code inside this block will be tread safe
// The code can inlcude synchonized methods
}
}
- Blocking Queues
LinkedBlockingQueue
PriorityBlockingQueue
Comentarios
Publicar un comentario