STIW3054-A172 / Main-Issues

1 stars 1 forks source link

Exercise_06 #7

Closed zhamri closed 6 years ago

zhamri commented 6 years ago

Rewrite the Java program from this example by Implementing Thread Executer.

diyanazaidi commented 6 years ago

package com.mycompany.issue;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author Diyana
 */
public class MyThread implements Runnable {
    private static final int MYPOOL = 10;
    public void run() {
        //for(int i=0;i<10;i++){

        System.out.println("running 1 " + Thread.currentThread().getName());
        System.out.println("running 2 " + Thread.currentThread().getName());
        System.out.println("running 3 " + Thread.currentThread().getName());

        //}
    }
    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(MYPOOL);

        for(int i=0;i<2;i++){
            Runnable r = new MyThread();
            executor.execute(r);
        }

        executor.shutdown();        
    }

}

Output : image

ZmahHata commented 6 years ago
package exercise05;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Exercise05 implements Runnable {
    public void run() {
        for(int x=0; x<10; x++){
        System.out.println("running 1" + Thread.currentThread().getName());
        System.out.println("running 2" + Thread.currentThread().getName());
        System.out.println("running 3" + Thread.currentThread().getName());
    }
    }

    public static void main(String args[]) {

        ExecutorService executor = Executors.newFixedThreadPool(5);
        for(int i=0;i<2;i++){
            Runnable worker = new Exercise05();
            executor.execute(worker);
        }

        Thread t1 = new Thread(new Exercise05());
        Thread t2 = new Thread(new Exercise05());
        System.out.println("Name of t1:" + t1.getName());
        System.out.println("Name of t2:" + t2.getName());
        System.out.println("id of t1:" + t1.getId());

        t1.setName("STIW3054");
        System.out.println("After changing name of t1:" + t1.getName());

        executor.shutdown();
    }
}

image

adamrustam commented 6 years ago

Code


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

    class MyThread1 implements Runnable {

    private static final int MYPOOL = 10;

    public void run() {
        //for(int i=0;i<10;i++){
        System.out.println("running 4"+ Thread.currentThread().getName());
        System.out.println("running 5"+ Thread.currentThread().getName());
        System.out.println("running 2"+ Thread.currentThread().getName());

    }

    public static void main (String args[]) {

         ExecutorService executor = Executors.newFixedThreadPool(MYPOOL);
         for (int i = 0; i < 10; i++) {
             Runnable worker = new MyThread1();
             executor.execute(worker);
           }
         executor.shutdown();
         while (!executor.isTerminated()) {
         }
         //System.out.println("Finished all threads");

    }

}

Output

screen shot 2018-03-25 at 12 33 28 pm

References https://www.journaldev.com/1069/threadpoolexecutor-java-thread-pool-example-executorservice

liiyanamegat commented 6 years ago

package rt_exercise06;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class RT_Exercise06 extends Thread{

     public void run() {
       for (int x = 0; x < 10; x++){
        System.out.println("running 1 " + Thread.currentThread().getName());
        System.out.println("running 2 " + Thread.currentThread().getName());
        System.out.println("running 3 " + Thread.currentThread().getName());
    }
     }

    public static void main(String args[]) {

        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new RT_Exercise06 ();
            executor.execute(worker);
          }
        executor.shutdown();
    }
}

Output image

AsadRazali commented 6 years ago

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyThread implements Runnable {

    public void run() {

           System.out.println("running...1 "+ Thread.currentThread().getName());
            System.out.println("running...2 "+ Thread.currentThread().getName());
            System.out.println("running...3 "+ Thread.currentThread().getName());
    }
    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(5);

            Runnable t1 = new MyThread();
             Runnable t2 = new MyThread();
            System.out.println("Name of t1:" + Thread.currentThread().getName());
            System.out.println("Name of t2:" + Thread.currentThread().getName());
            System.out.println("id of t1:" +   Thread.currentThread().getId());

             for(int i=0;i<10;i++)
             {
            executor.execute(t1);
            executor.execute(t2);
           }

        executor.shutdown();     
          while (!executor.isTerminated()) {
        }
          Thread.currentThread().setName("STIW3054");
          System.out.println("After changing name of t1:" + Thread.currentThread().getName());
    }

}

1

umarabdullah commented 6 years ago
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.stiw3054_assignment.excercise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author abdullah
 */
class  ReWrite extends Thread implements Runnable {

    private static Integer pool = 10;
    static Thread t1;
    static Thread t2;
    public void run() {

        System.out.println("running.. ");

    }
    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(pool);

             t1 = new ReWrite();
            executor.execute(t1);
             t2 = new ReWrite();
            executor.execute(t2);

            System.out.println("running  " + Thread.currentThread().getName());

            System.out.println("Name of t1:" + t1.getName());
            System.out.println("Name of t2:" + t2.getName());
            System.out.println("id of t1:" + t1.getId());

            t1.setName("STIW3054");
            System.out.println("After changing name of t1:" + t1.getName());

        executor.shutdown();        
    }

}
screen shot 2018-03-25 at 12 36 54 pm
kzkit commented 6 years ago

Source Code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mythread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

    class MyExecuter implements Runnable {

    private static final int MYPOOL = 10;

    public void run() {

        for(int i=1;i<4;i++){
        if ( i==2){
         Thread.currentThread().setName("STIW3054");
        }
        System.out.println("running"+i+ Thread.currentThread().getName());
        System.out.println("running"+i+ Thread.currentThread().getName());
        System.out.println("running"+i+ Thread.currentThread().getName());
        }
    }

    public static void main (String args[]) {

         ExecutorService executor = Executors.newFixedThreadPool(MYPOOL);
         for (int i = 0; i < 10; i++) {
             Runnable tryRun = new MyExecuter();
             executor.execute(tryRun);

           }
         executor.shutdown();
         while (!executor.isTerminated()) {
         }
         System.out.println("Threads are done.");   
    }
}

Output 👍 issue6

Nurzyra commented 6 years ago
package exercise6;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Exercise6 implements Runnable{

    public void run() {
        for(int x=0; x<10; x++){
        System.out.println("running 1" + Thread.currentThread().getName());
        System.out.println("running 2" + Thread.currentThread().getName());
        System.out.println("running 3" + Thread.currentThread().getName());
        }
    }

    public static void main(String args[]) {

        ExecutorService executor = Executors.newFixedThreadPool(5);
        for(int i=0;i<2;i++){
            Runnable worker = new Exercise6();
            executor.execute(worker);
        }

        Thread t1 = new Thread(new Exercise6());
        Thread t2 = new Thread(new Exercise6());
        System.out.println("Name of t1:" + t1.getName());
        System.out.println("Name of t2:" + t2.getName());
        System.out.println("id of t1:" + t1.getId());

        t1.setName("STIW3054");
        System.out.println("After changing name of t1:" + t1.getName());

        executor.shutdown();
    }
}

output : 2

AdlanShahjehan commented 6 years ago
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main implements Runnable {
    private static final int MYPOOL = 10;
    public void run() {

        System.out.println("Running 1 " + Thread.currentThread().getName());
        System.out.println("Running 2 " + Thread.currentThread().getName());
        System.out.println("Running 3 " + Thread.currentThread().getName());

    }
    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(MYPOOL);

        for(int i=1;i<10;i++){
            Runnable j = new Main();
            executor.execute(j);
        }
        executor.shutdown();        
    }
}

Output 6