Issuu on Google+

Executor : : ‍  ؏ ؼ‏Executor ‍  اـ‏ 1. import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; 2. in the main (String [] args) ExecutorService

‍دا اـ‏

runner = Executors. newFixedThreadPool( int ‍;) ع‏

Executor Variable ‍ا ا‏

Number of threads to be executed ‍ ا    ďş•ه‏threads ‍د‏

Executor Methods:

newFixedThreadPool (3) ;

---> 3 is the number of threads

creates a pool consisting of a fixed number of threads as indicated by the method argument. â&#x20AC;ŤŘ§Řłâ&#x20AC;Ź%,â&#x20AC;Ť" اâ&#x20AC;Ź- .â&#x20AC;Ť اŮ&#x20AC; اâ&#x20AC;Ź/â&#x20AC;Ť Ů&#x2C6;Ů&#x2021;ا اŘŻ ŘŻâ&#x20AC;Źthreadsâ&#x20AC;Ť" اŮ&#x201E;â&#x20AC;Ź# â&#x20AC;ŤŘŻâ&#x20AC;Ź# â&#x20AC;Ť ŘĄ ŘŻâ&#x20AC;Ź%â&#x20AC;Ť& اâ&#x20AC;Ź# â&#x20AC;Ť( Ů&#x2020;â&#x20AC;Ź# )*â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź These threads are used by thread Executor to execute the runnables. If method execute ( ) is called and all the threads in the ExecutorService are being used, the Runnable will be placed in a queue and assigned to the first thread that completes its previous task. newFixedThreadPool( ) 3 â&#x20AC;ŤŘ§Řłâ&#x20AC;Ź%,â&#x20AC;Ť" اâ&#x20AC;Ź- /â&#x20AC;Ť اŘŻ اŮ&#x160; ďş&#x2022; ذآâ&#x20AC;Ź7- execute( )  1â&#x20AC;Ť اŮ&#x2020; ďş&#x2022;â&#x20AC;Ź2,â&#x20AC;ŤŘ§â&#x20AC;Ź â&#x20AC;Ť;Ů&#x2020;â&#x20AC;Ź3 <#â&#x20AC;ŤŘŻ" ďş&#x2022; ا =اâ&#x20AC;Ź.â&#x20AC;Ť اâ&#x20AC;Źthreads â&#x20AC;Ť Ů&#x2C6;ŘŻ اŮ&#x201E;â&#x20AC;Ź9â&#x20AC;Ť" Ř°â&#x20AC;Ź# &â&#x20AC;Ť أآâ&#x20AC;Źexecute ( ) â&#x20AC;Ť ﺥ Ů&#x201E; ďş&#x2022; ا  ŘĄâ&#x20AC;Ź3 â&#x20AC;ŤŮ&#x2C6;Ů&#x201E;â&#x20AC;ŹE /â&#x20AC;Ť   ا ŘŻâ&#x20AC;ŹF- queue >3%?# 3 @Aâ&#x20AC;Ť   Ů&#x2C6;â&#x20AC;Ź/â&#x20AC;Ť ďş&#x2022;â&#x20AC;ŹB# Câ&#x20AC;Ť اâ&#x20AC;ŹD â&#x20AC;Ť اŮ&#x160;â&#x20AC;ŹRunnable task <D. <â&#x20AC;Ť ďş&#x2022;â&#x20AC;Źthread newCachedThread ( ) ;

---> returns an ExecutorService that creates new threads as they are needed by the application.

â&#x20AC;Ťات اﺥ  ŘŹâ&#x20AC;Ź# â&#x20AC;ŤŘŻâ&#x20AC;Ź- â&#x20AC;Ť ŘŻ Ů&#x2021;ا اŘŻâ&#x20AC;ŹThreads â&#x20AC;Ť" اŮ&#x201E;â&#x20AC;Ź# â&#x20AC;Ť *) ŘŻâ&#x20AC;Ź/â&#x20AC;ŤŮ&#x2C6;Řąâ&#x20AC;Ź- â&#x20AC;Ť اŮ&#x160;â&#x20AC;ŹExecutorService â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź â&#x20AC;ŤŘŠâ&#x20AC;ŹI â&#x20AC;Ť *ŘĄ Ů&#x2C6;اﺥŘŠâ&#x20AC;Źthread â&#x20AC;Ť اâ&#x20AC;ŹB# Câ&#x20AC;Ť اﺥ ŘŹ اâ&#x20AC;Ź.Dâ&#x20AC;Ť Ů&#x2C6; آâ&#x20AC;Źthreads  B# Câ&#x20AC;ŤŘ§â&#x20AC;Ź


runner. execute ( task );

Executor Variable

implements Runnable

-----> creates a new Thread inside the ExecutorService to run the Runnable passed to it as an argument â&#x20AC;Ť   اŮ&#x20AC;â&#x20AC;ŹExecutorService â&#x20AC;Ť ŘĄâ&#x20AC;Ź%â&#x20AC;ŤŘŠ دا اŮ&#x20AC;â&#x20AC;ŹI Thread )*â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź â&#x20AC;ŤŘ§Řłâ&#x20AC;Ź%,â&#x20AC;Ť" اâ&#x20AC;Ź- /.â&#x20AC;Ť اŮ&#x160;   ďş&#x2022;â&#x20AC;ŹRunnable

runner. shutdown ( ) ;

------> It will end each thread in ExecutorService runner as soon as each Thread finishes executing its Runnable. â&#x20AC;Ť" ďş&#x2022;â&#x20AC;Ź# Threads â&#x20AC;Ť ا < ŘĄ اŮ&#x20AC;â&#x20AC;Ź- â&#x20AC;Ť ﺾ؊â&#x20AC;ŹC# runner J .â&#x20AC;Ť Ů&#x2C6;Ů&#x2021; ا اâ&#x20AC;ŹExecutorService â&#x20AC;Ť اŮ&#x20AC;â&#x20AC;Ź. <â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź <- â&#x20AC;ŤŮ&#x2026;â&#x20AC;Ź%Mâ&#x20AC;Ť ا  ďş&#x2022;â&#x20AC;ŹRunnable >.<.â&#x20AC;ŤŘ§â&#x20AC;Ź :â&#x20AC;Ť& Ů&#x201E;â&#x20AC;Ź#

Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

// Fig. 23.5: RunnableTester.java // Multiple threads printing at different intervals. import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; public class RunnableTester { public static void main( String[] args ) { // create and name each runnable PrintTask task1 = new PrintTask( "thread1" ); PrintTask task2 = new PrintTask( "thread2" ); PrintTask task3 = new PrintTask( "thread3" );

tasks that â&#x20AC;ŤŮ&#x2021; ďş&#x2022;Ű&#x152; ا  ŘŞâ&#x20AC;Ź implements Runnable

System.out.println( "Starting threads" ); // create ExecutorService to manage threads ExecutorService threadExecutor = Executors.newFixedThreadPool( 3 ); // start threads and place in threadExecutor.execute( task1 threadExecutor.execute( task2 threadExecutor.execute( task3

runnable state ); // start task1 ); // start task2 ); // start task3

threadExecutor.shutdown(); // shutdown worker threads System.out.println( "Threads started, main ends\n" ); } // end main } // end class RunnableTester

 execute â&#x20AC;ŤŮ&#x2021; ا  ŘĄ اŘŻâ&#x20AC;Ź â&#x20AC;ŤŮ&#x2021; ا  ŘŞ  ŘŻ اتâ&#x20AC;Ź !â&#x20AC;Ť اŘŻ اŘą اâ&#x20AC;Ź#â&#x20AC;ŤŘ§  ŘĄŮ&#x2021; Ů&#x2021; â&#x20AC;Ź newFixedThreadPool


Locks : â&#x20AC;Ť  ŘŹ ŘĽâ&#x20AC;ŹLock â&#x20AC;Ť  اŮ&#x20AC;â&#x20AC;Ź 1. import java.util.concurrent.locks.lock; import java.util.concurrent.locks.ReentrantLock; 2. private

Lock

lockObject ( ) ;

3. inside the constructor : lockObject = new ReentrantLock ( true ); Class that is a basic implementation of the Lock interface takes a boolean argument that specifies whether the lock has a fairness policy true : fairness policy states that the longest waiting thread will aquire the lock when it is available. Mâ&#x20AC;Ť اâ&#x20AC;ŹRS   â&#x20AC;Ť" Ů&#x2021; اâ&#x20AC;Ź#Râ&#x20AC;Ť اâ&#x20AC;ŹFâ&#x20AC;Ť" ﺥâ&#x20AC;Ź# â&#x20AC;Ť ŘąŮ&#x2039;اâ&#x20AC;ŹQ â&#x20AC;ŤŘ˘& اâ&#x20AC;ŹEâ&#x20AC;Ť اâ&#x20AC;Źthread â&#x20AC;Ť اŮ&#x20AC;â&#x20AC;ŹO M â&#x20AC;ŤŘŁ اا> اŮ&#x160;â&#x20AC;ŹC# 7Nâ&#x20AC;Ťďş&#x2022;â&#x20AC;Ź . â&#x20AC;Ť اŮ&#x2030;â&#x20AC;Źthread "# â&#x20AC;ŤŘ˛â&#x20AC;Ź%S# Uâ&#x20AC;Ť ﺥŮ&#x2039; Ů&#x2C6;â&#x20AC;Ź# â&#x20AC;Ť ﺥ > آ Ů&#x2020;â&#x20AC;Ź3 false: there is no guarantee as to which waiting thread will aquire the lock when it is available. â&#x20AC;Ť ŘąŮ&#x2039;اâ&#x20AC;ŹQ â&#x20AC;ŤŘ˘&Ů&#x2021; اâ&#x20AC;ŹE Qâ&#x20AC;Ť ŘŻŮ&#x2C6;Ů&#x2020; اâ&#x20AC;Źthread â&#x20AC;ŤŮ&#x160;â&#x20AC;ŹE â&#x20AC;ŤŘ˛â&#x20AC;Ź%S# Uâ&#x20AC;Ť ﺥŮ&#x2039; Ů&#x2C6;â&#x20AC;Ź# â&#x20AC;Ť ﺥ > اذا آ Ů&#x2020;â&#x20AC;Ź3 Mâ&#x20AC;Ť ŘĄ اâ&#x20AC;ŹXâ&#x20AC;Ť  اâ&#x20AC;Ź Lock methods: lock â&#x20AC;ŤŮ&#x20AC;â&#x20AC;ŹD >- â&#x20AC;Ť اâ&#x20AC;Źmethods â&#x20AC;ŤŘ§Ů&#x20AC;â&#x20AC;Ź Condition variable must be associated with a lock and created by calling newCondition( ) method. newCondition ( ) â&#x20AC;ŤŘŻâ&#x20AC;Ź%&# â&#x20AC;ŤŮ&#x201E;â&#x20AC;ŹY "# Condition Variable J # â&#x20AC;Ť" ا* ŘĄâ&#x20AC;Ź# -, Mâ&#x20AC;Ť اâ&#x20AC;ŹRS lockObject. newCondition ( ); returns an object that implements the condition interface. Lock Object Variable


lockObject. lock( ) ;

------> obtain the lock

Mâ&#x20AC;Ť اâ&#x20AC;ŹRS

a thread call the lockâ&#x20AC;&#x2122;s lock ( ) method to obtain the lock , Once the lock has been Obtained by one thread the lock Object will not allow another thread to obtain the lock until the first thread release the lock. â&#x20AC;Ť Ů&#x2021;اâ&#x20AC;ŹRSâ&#x20AC;Ť ŘŁŮ&#x2020; ďş&#x2022;â&#x20AC;Źthread â&#x20AC;ŤŮ&#x160;â&#x20AC;ŹE Mâ&#x20AC;Ť اâ&#x20AC;ŹZ.1 " thread â&#x20AC;Ť اŮ&#x160;â&#x20AC;ŹC "# Mâ&#x20AC;Ť اâ&#x20AC;ŹRSâ&#x20AC;Ť ﺥ Ů&#x201E; ďş&#x2022; ﺥâ&#x20AC;Ź3â&#x20AC;Ť Ů&#x2C6;â&#x20AC;ŹMâ&#x20AC;Ť اâ&#x20AC;ŹRS  1â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź . <D. thread â&#x20AC;Ť اŮ&#x2020; ďş&#x2022;< اŮ&#x201E;â&#x20AC;Ź- ,â&#x20AC;Ť اâ&#x20AC;ŹMâ&#x20AC;ŤŘ§â&#x20AC;Ź lockObject.unlock ( ) ; -----> release the lock

Mâ&#x20AC;Ť اâ&#x20AC;ŹRSâ&#x20AC;Ť ŘĄ ﺥâ&#x20AC;ŹJ,

thread release the lock and the highest priority waiting thread attempting to lock the object proceeds. <D. .(â&#x20AC;Ť> Ů&#x2C6;ďş&#x2022;â&#x20AC;Ź%â&#x20AC;ŤŘ˘& ŘŁŮ&#x2C6;â&#x20AC;ŹEâ&#x20AC;Ť اâ&#x20AC;Ź/RS  Mâ&#x20AC;Ť اâ&#x20AC;ŹRSâ&#x20AC;Ť ďş&#x2022; ﺥâ&#x20AC;ŹQ â&#x20AC;Ť ا  ďş&#x2022;â&#x20AC;Źthreads â&#x20AC;Ť Ů&#x2C6;اŮ&#x20AC;â&#x20AC;ŹMâ&#x20AC;Ť اâ&#x20AC;ŹRSâ&#x20AC;Ť ŘĄ ﺥâ&#x20AC;ŹJ,  1â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź .â&#x20AC;Ť Řąâ&#x20AC;ŹQ â&#x20AC;Ť ﺥ > اâ&#x20AC;Ź3 [A%â&#x20AC;Ť ďş&#x2022;â&#x20AC;Źthreads â&#x20AC;Ť> اŮ&#x20AC;â&#x20AC;ŹM-â&#x20AC;ŤŮ&#x2C6;â&#x20AC;Ź If there are several methods try to obtain the lock object. Only, One thread may obtain the lock at a time, all other threads attempting to obtain the lock contained in the same object are placed in the waiting state for the lock.


Condition : â&#x20AC;Ť  ŘŹ اâ&#x20AC;ŹCondition â&#x20AC;Ť  اŮ&#x201E;â&#x20AC;Ź 1. import java.util.concurrent.locks.Condition; 2. private Condition suspend; (J .â&#x20AC;Ť)ا اâ&#x20AC;Ź 3. In the constructor

â&#x20AC;ŤŘŻŘ§ اŮ&#x20AC;â&#x20AC;Ź

suspend = lockObject. newCondition( ); Lock â&#x20AC;ŤŘšâ&#x20AC;Ź% "# J # This method returns an object that implements the Condition interface

4. Methods of condition Condition Variable

suspend. await ( );

Condition â&#x20AC;Ť دا اŮ&#x20AC;â&#x20AC;Źmethods â&#x20AC;ŤŘ§Ů&#x20AC;â&#x20AC;Ź ------> suspend Thread execution thread. %â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź

3 Thread â&#x20AC;Ť[ اŮ&#x201E;â&#x20AC;ŹOâ&#x20AC;ŤŘŻ Ů&#x2C6;ďş&#x2022;â&#x20AC;Ź%I%.â&#x20AC;Ť اâ&#x20AC;ŹMâ&#x20AC;Ť اâ&#x20AC;ŹRâ&#x20AC;Ť Ů&#x2C6;ďş&#x2022;â&#x20AC;ŹCondtion â&#x20AC;ŤŘšâ&#x20AC;Ź% "# J # â&#x20AC;Ť Řą داâ&#x20AC;ŹQ Y  1â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź waiting > â&#x20AC;ŤďşĄâ&#x20AC;Ź Called to wait on a condition variable and releases the associated lock and places the thread in the waiting state.

suspend. signal ( ); -----> resume Thread <D. 3 . 1â&#x20AC;Ť ďş&#x2022;â&#x20AC;Źthread .(â&#x20AC;Ť Řą ďş&#x2022;â&#x20AC;ŹQ â&#x20AC;Ť ﺥ > اâ&#x20AC;Ź3  â&#x20AC;ŤŮ&#x2030; اâ&#x20AC;Ź,â&#x20AC;Ť اâ&#x20AC;Źthread â&#x20AC;Ť اŮ&#x201E;â&#x20AC;ŹSâ&#x20AC;Ť< Ů&#x2C6; ďş&#x2022;â&#x20AC;ŹD. thread â&#x20AC;Ť ďş&#x2022;< اŮ&#x201E;â&#x20AC;Ź#  1â&#x20AC;Ťďş&#x2022;â&#x20AC;Ź . <D. Called when the runnable thread completes a task and determines that the waiting thread can now continue.


java