logo

Multithread in Java

Il multithreading è una funzionalità Java che consente l'esecuzione simultanea di due o più parti di un programma per il massimo utilizzo della CPU. Ciascuna parte di tale programma è chiamata thread. Pertanto, i thread sono processi leggeri all'interno di un processo.

I thread possono essere creati utilizzando due meccanismi:



  1. Estendere la classe Thread
  2. Implementazione dell'interfaccia eseguibile

Creazione di thread estendendo la classe Thread
Creiamo una classe che estende il java.lang.Thread classe. Questa classe sovrascrive il metodo run() disponibile nella classe Thread. Un thread inizia la sua vita all'interno del metodo run(). Creiamo un oggetto della nostra nuova classe e chiamiamo il metodo start() per avviare l'esecuzione di un thread. Start() richiama il metodo run() sull'oggetto Thread.

Giava








// Java code for thread creation by extending> // the Thread class> class> MultithreadingDemo>extends> Thread {> >public> void> run()> >{> >try> {> >// Displaying the thread that is running> >System.out.println(> >'Thread '> + Thread.currentThread().getId()> >+>' is running'>);> >}> >catch> (Exception e) {> >// Throwing an exception> >System.out.println(>'Exception is caught'>);> >}> >}> }> // Main Class> public> class> Multithread {> >public> static> void> main(String[] args)> >{> >int> n =>8>;>// Number of threads> >for> (>int> i =>0>; i MultithreadingDemo object = new MultithreadingDemo(); object.start(); } } }>

inversione delle corde in c
>

>

Produzione

Thread 15 is running Thread 14 is running Thread 16 is running Thread 12 is running Thread 11 is running Thread 13 is running Thread 18 is running Thread 17 is running>

Creazione del thread implementando l'interfaccia eseguibile
Creiamo una nuova classe che implementa l'interfaccia java.lang.Runnable e sovrascrive il metodo run(). Quindi istanziamo un oggetto Thread e chiamiamo il metodo start() su questo oggetto.

Giava




// Java code for thread creation by implementing> // the Runnable Interface> class> MultithreadingDemo>implements> Runnable {> >public> void> run()> >{> >try> {> >// Displaying the thread that is running> >System.out.println(> >'Thread '> + Thread.currentThread().getId()> >+>' is running'>);> >}> >catch> (Exception e) {> >// Throwing an exception> >System.out.println(>'Exception is caught'>);> >}> >}> }> // Main Class> class> Multithread {> >public> static> void> main(String[] args)> >{> >int> n =>8>;>// Number of threads> >for> (>int> i =>0>; i Thread object = new Thread(new MultithreadingDemo()); object.start(); } } }>

>

>

Produzione

Thread 13 is running Thread 11 is running Thread 12 is running Thread 15 is running Thread 14 is running Thread 18 is running Thread 17 is running Thread 16 is running>

Classe thread e interfaccia eseguibile

  1. Se estendiamo la classe Thread, la nostra classe non può estendere nessun'altra classe perché Java non supporta l'ereditarietà multipla. Ma, se implementiamo l'interfaccia Runnable, la nostra classe può comunque estendere altre classi base.
  2. Possiamo ottenere le funzionalità di base di un thread estendendo la classe Thread perché fornisce alcuni metodi integrati come yield(), interrupt() ecc. che non sono disponibili nell'interfaccia Runnable.
  3. L'uso di runnable ti darà un oggetto che può essere condiviso tra più thread.