" /> multithreading - "implements Runnable" vs "extend Thread" en Java | 2022 codeprofesseur

multithreading - "implements Runnable" vs "extend Thread" en Java

Mots clés : javamultithreadingrunnableimplementsjava-threadsjava

meilleur 2 Réponses multithreading - "implements Runnable" vs "extend Thread" en Java

vote vote

91

Future<?> f = new FutureTask<Object>(runnable, null) 
new FutureTask<Object>(threadA, null) 
ExecutorService es = Executors.newCachedThreadPool(); es.execute(new ThreadA()); 
vote vote

80

//Implement Runnable Interface... class ImplementsRunnable implements Runnable {      private int counter = 0;      public void run() {         counter++;         System.out.println("ImplementsRunnable : Counter : " + counter);     } }  //Extend Thread class... class ExtendsThread extends Thread {      private int counter = 0;      public void run() {         counter++;         System.out.println("ExtendsThread : Counter : " + counter);     } }      //Use the above classes here in main to understand the differences more clearly... public class ThreadVsRunnable {      public static void main(String args[]) throws Exception {         // Multiple threads share the same object.         ImplementsRunnable rc = new ImplementsRunnable();         Thread t1 = new Thread(rc);         t1.start();         Thread.sleep(1000); // Waiting for 1 second before starting next thread         Thread t2 = new Thread(rc);         t2.start();         Thread.sleep(1000); // Waiting for 1 second before starting next thread         Thread t3 = new Thread(rc);         t3.start();          // Creating new instance for every thread access.         ExtendsThread tc1 = new ExtendsThread();         tc1.start();         Thread.sleep(1000); // Waiting for 1 second before starting next thread         ExtendsThread tc2 = new ExtendsThread();         tc2.start();         Thread.sleep(1000); // Waiting for 1 second before starting next thread         ExtendsThread tc3 = new ExtendsThread();         tc3.start();     } } 
ImplementsRunnable : Counter : 1 ImplementsRunnable : Counter : 2 ImplementsRunnable : Counter : 3 ExtendsThread : Counter : 1 ExtendsThread : Counter : 1 ExtendsThread : Counter : 1 

Questions similaires