NaoDesu

ContohMultiThread

Feb 20th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.util.concurrent.ExecutorService;
  2. import java.util.concurrent.Executors;
  3.  
  4. public class ContohMultiThread {
  5.     public static void main(String args[]) {
  6.  
  7.         ExecutorService es = Executors.newFixedThreadPool(4);
  8.  
  9.         es.execute(new MyThread("A"));
  10.         es.execute(new MyThread("B"));
  11.         es.execute(new MyThread("C"));
  12.         es.execute(new MyThread("D"));
  13.  
  14.         es.shutdown();
  15.     }
  16. }
  17.  
  18. class MyThread implements Runnable {
  19.     String name;
  20.  
  21.     MyThread(String n) {
  22.         name = n;
  23.         new Thread(this);
  24.     }
  25.  
  26.     public void run() {
  27.         if (name=="A"){
  28.             for (int i=1;i<=10;i++){
  29.                 System.out.println(i);
  30.                 try {
  31.                     Thread.sleep(1000);
  32.                 } catch (InterruptedException e) {
  33.                     // TODO Auto-generated catch block
  34.                     e.printStackTrace();
  35.                 }
  36.             }
  37.         }
  38.         if (name=="B"){
  39.             for (int i=10;i<=20;i++){
  40.                 System.out.println(i);
  41.                 try {
  42.                     Thread.sleep(1000);
  43.                 } catch (InterruptedException e) {
  44.                     // TODO Auto-generated catch block
  45.                     e.printStackTrace();
  46.                 }
  47.             }
  48.         }
  49.         if (name=="C"){
  50.             for (int i=20;i<=30;i++){
  51.                 System.out.println(i);
  52.                 try {
  53.                     Thread.sleep(1000);
  54.                 } catch (InterruptedException e) {
  55.                     // TODO Auto-generated catch block
  56.                     e.printStackTrace();
  57.                 }
  58.             }
  59.         }
  60.         if (name=="D"){
  61.             for (int i=30;i<=40;i++){
  62.                 System.out.println(i);
  63.                 try {
  64.                     Thread.sleep(1000);
  65.                 } catch (InterruptedException e) {
  66.                     // TODO Auto-generated catch block
  67.                     e.printStackTrace();
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment