piffy

EsempioThread

Sep 1st, 2014 (edited)
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. public class ThreadEsempio extends Thread {
  2. String nome;
  3.     public ThreadEsempio(String nome) {
  4.         this.nome=nome;
  5.     }
  6. @Override
  7.     public void run() {
  8.         for (int i = 0; i < 10; i++) {
  9.             System.out.println(i + " " + nome);
  10.             try {
  11.                 sleep((int)(Math.random() * 1000));
  12.             } catch (InterruptedException e) {}
  13.         }
  14.         System.out.println(nome + " terminato");
  15.     }
  16.  
  17.     public static void main(String[] args) throws InterruptedException {
  18.         ThreadEsempio t = new ThreadEsempio("Pippo");
  19.         t.start();
  20.         ThreadEsempio t1= new ThreadEsempio("Pluto");
  21.         t1.start();
  22.         t.join();t1.join();
  23.         System.out.println("Programma terminato");
  24.        
  25.        
  26.     }
  27.    
  28. }
  29.    
  30.    
  31.    
Add Comment
Please, Sign In to add comment