STIW3054-A191 / Main-Issues

3 stars 2 forks source link

Multiplication Table #10

Open zhamri opened 4 years ago

zhamri commented 4 years ago

Instruction:

You have to develop a program that creates and runs 3 threads. Each thread calculates and prints the multiplication table of a number between 1 and 3. The example of the output is shown below (Hints: MUST create TWO (2) classes).

Thread-0: 1 1 = 1 Thread-2: 3 1 = 3 Thread-2: 3 2 = 6 Thread-2: 3 3 = 9 Thread-1: 2 1 = 2 Thread-1: 2 2 = 4 Thread-1: 2 3 = 6 Thread-0: 1 2 = 2 Thread-0: 1 * 3 = 3

Submission:

  1. Java code
  2. Screenshot of the output
weiditan commented 4 years ago

TAN WEI DI 259296

public class MultiplicationTable {

    public static void main(String []args){
        for(int i=1;i<=3;i++) {
            Runnable runnable = new myThread(i);
            Thread thread = new Thread(runnable);
            thread.setName("Thread-"+(i-1));
            thread.start();
        }
    }
}

class myThread implements Runnable {
    private int threadNo;

    myThread(int threadNo){
        this.threadNo = threadNo;
    }

    public void run(){
        for(int i=1;i<=3;i++){
            System.out.println(Thread.currentThread().getName()+": "+threadNo+" * "+i+" = "+(threadNo*i));
        }
    }
}

Capture1

jasonway96 commented 4 years ago

Name: Lim Guang Way Matric No: 256750

[ CLASS MULTIPLY ]

package Issue10;

import java.util.concurrent.locks.ReentrantLock;

public class Multiply
{
    public static ReentrantLock lock = new ReentrantLock();

    public static void main () throws Exception
    {

        Thread t1 = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for(int i=1;i<4;i++)
                {
                    lock.lock();
                    int total = i * 1;
                    System.out.println("Thread-0: "+i+" * 1 = "+ total );
                    lock.unlock();
                }
            }
        });

        Thread t2 = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for(int i=1;i<4;i++)
                {
                    lock.lock();
                    int total = i * 2;
                    System.out.println("Thread-1: "+i+" * 2 = "+ total );
                    lock.unlock();
                }
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run()
            {
                for(int i=1;i<4;i++)
                {
                    lock.lock();
                    int total = i * 3;
                    System.out.println("Thread-2: "+i+" * 3 = "+ total );
                    lock.unlock();
                }
            }
        });

        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        t3.join();
    }

}

[ CLASS MAIN ]

package Issue10;

import java.io.IOException;

public class Main extends Multiply
{
    public static void main (String[] args) throws Exception
    {
        Multiply.main();
    }
}

[ SCREENSHOT ] image

TanChengYi commented 4 years ago

Tan Cheng Yi 253814

package com.tanchengyi;

import java.util.Random;

public class issues10 {
    public static void main(String[] args) {
        Thread[] thread=new multiplication[3];
        for(int i=0;i<3;i++){
            number num =new number();
            thread[i]=new multiplication(num.getNum());
            thread[i].run();
        }
    }
}

class number{

    public int getNum() {
        Random rand =new Random();
        int num=rand.nextInt(4);
        while(num<1){
            num=rand.nextInt(3);
        }
        return num;
    }
}
class multiplication extends Thread{
    int num;
    multiplication(int num){
        this.num=num;
    }
    @Override
    public void run() {
        synchronized (this) {
            for (int i = 1; i <= 3; i++) {
                System.out.println(getName() + ":" + num+ "*" + i + "=" + (num* i));
            }
        }
    }
}

Output image

PhuahMeiQi commented 4 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 Issue;

/**
 *
 * @author MICKY
 */

/**
 *
 * @author MICKY
 */
class Thread1 extends Thread {

    @Override
    public void run() {
        try {
            for (int i = 1; i <=3; i++) {
                int result = i*1;
                System.out.println(Thread.currentThread().getName()+":"+i+"*1="+result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Thread2 extends Thread {

    @Override
    public void run() {
        try {
            for (int i = 1; i <=3; i++) {
                int result = i*2;
                System.out.println(Thread.currentThread().getName()+":"+i+"*2="+result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Thread3 extends Thread {

    @Override
    public void run() {
        try {
            for (int i = 1; i <=3; i++) {
                int result = i*3;

                System.out.println(Thread.currentThread().getName()+":"+i+"*3="+result);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class issuetenth  {

    public static void main(String[] args) {
        Thread1 t1 = new Thread1();
        Thread2 t2= new Thread2();
        Thread3 t3= new Thread3();
        t1.start();
        t2.start();
        t3.start();
    }

}

image

WwLuo-1024 commented 4 years ago

Name: Wang Luo Matric No: 251230

CLASS Thread01 package threadtest;

/**
 *
 * @author master lab
 */
    public class Thread01 extends Thread{
        public void run(){
            try{
            for(int i = 1;i <= 3;i++){
            System.out.println(Thread.currentThread().getName()+":"+1+" * "+i+" = "+1 * i);
                    }
            }catch(Exception e){
                    e.printStackTrace();
                    }
        }
    }
    class Thread02 extends Thread{

        public void run(){
            try{
            for(int i = 1;i <= 3;i++){
            System.out.println(Thread.currentThread().getName()+":"+2+" * "+i+" = "+2 * i);

                    }
        }catch(Exception e){
        e.printStackTrace();
    }
        }
    }
    class Thread03 extends Thread{
        public void run(){
            try{
            for(int i = 1;i <= 3;i++){
            System.out.println(Thread.currentThread().getName()+":"+3+" * "+i+" = "+3 * i);

                    }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    } 

CLASS TEST
package threadtest;
/**
 *
 * @author master lab
 */
public class Test extends Thread {
    public static void main(String[] agrs) throws InterruptedException{
        Thread01 th1 = new Thread01();
        Thread02 th2 = new Thread02();
        Thread03 th3 = new Thread03();
        th1.start();
        Thread.sleep(100);
        th2.start();
        Thread.sleep(100);
        th3.start();
        Thread.sleep(100);
    }
}

result

farisleh commented 4 years ago

Mohammad Faris Bin Ahmad Shukri 255312


/*
 * 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 multiplication;

/**
 *
 * @author master lab
 */
public class Multiplication {

    public static void main(String[] args) {
        Runnable r = new Runnable1();
        Thread t = new Thread(r);
        Runnable r2 = new Runnable2();
        Thread t2 = new Thread(r2);
        Runnable r3 = new Runnable3();
        Thread t3 = new Thread(r3);
        t.start();
        t2.start();
        t3.start();
    }
}

class Runnable2 implements Runnable{
    public void run(){
        for(int i=1;i<=3;i++) {
            int result = i*2;
            System.out.println("Thread-1:"+i+"*2="+result);
        }
    }
}

class Runnable1 implements Runnable{
    public void run(){
        for(int i=1;i<=3;i++) {
           int result = i*1;
           System.out.println("Thread-0:"+i+"*1="+result);
        }
    }
}

class Runnable3 implements Runnable{
    public void run(){
        for(int i=1;i<=3;i++) {
           int result = i*3;
           System.out.println("Thread-2:"+i+"*3="+result);
        }
    }
}

issues8

coNNectGan commented 4 years ago

GAN LIAN JIE 255108

import java.io.IOException;

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

public class threadtest02 {

    public static void main (String[] args) throws IOException {

        ExecutorService service =  Executors.newFixedThreadPool(3);

        for (int i=1; i <=3;i++){
            service.execute(new IOTask());
        }

    }
    static class IOTask implements Runnable {

       int num1=1;
       int num2=1;

        public void run() {
            for (int i=1; i <=3;++i){
                System.out.printf(Thread.currentThread().getName()+":"+"%d * %d = %d \n", num1,num2 ,num1 * num2);
                num1++;
                num2++;

            }
        }

    }
}

outputReal

aida27 commented 4 years ago
import java.util.Random;

public class thread extends Thread {

    public void run() {
        try {
            for (int x = 0; x < 3; x++) {

                Random random = new Random();

                int r1 = random.nextInt(3) + 1;
                int r2 = random.nextInt(3) + 1;

                int result = r1 * r2;

                System.out.println(Thread.currentThread().getName() + r1 + " * " + r2 + " = " + result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main() throws Exception {

        thread t1 = new thread();
        thread t2 = new thread();
        thread t3 = new thread();

        t1.setName("Thread-0: ");
        t2.setName("Thread-1: ");
        t3.setName("Thread-2: ");

        t1.start();
        t2.start();
        t3.start();
    }
}

public class main extends thread
{
    public static void main (String[] args) throws Exception
    {
        thread.main();
    }
}
Capture
sohcheefung commented 4 years ago
class t implements Runnable{
    int n,n2,n3;
    t(int n, int n2, int n3){
        this.n=n;
        this.n2=n2;
        this.n3=n3;
    }
    public void run(){
        for(int i=1;i<=3;i++){
            switch(i){
            case 1:
            System.out.println(Thread.currentThread().getName()+":"+n+"*"+i+"="+(n*i));
            break;
            case 2:
            System.out.println(Thread.currentThread().getName()+":"+n+"*"+i+"="+(n2*i));
            break;
            case 3:
            System.out.println(Thread.currentThread().getName()+":"+n+"*"+i+"="+(n3*i));
            break;
                } 
            }
    }
}

public class JavaApplication8 {

    public static void main(String[] args) {
        int n = 1;
        int n2 = 2;
        int n3 = 3;
        Thread t1 = new Thread(new t(n,n2,n3),"Thread-0");
        Thread t2 = new Thread(new t(n,n2,n3),"Thread-1");
        Thread t3 = new Thread(new t(n,n2,n3),"Thread-2");

        t1.start();
        t2.start();
        t3.start();

    }
} 

Untitled

liviniesh commented 4 years ago
class thread1 extends Thread{
      @Override
    public void run(){

    try
     {
        int i=1 ;

          for (int j=1 ;j<4;j++)
        {
           System.out.println("Thread 0: " +i+"*"+j+"="+ (i*j));
           Thread.sleep(1000);
        }
     }
     catch(InterruptedException e)
     {
        System.out.println("my thread interrupted");
     }
   }
}

class thread2 extends Thread{
      @Override
    public void run(){

    try
     {
        int i=2 ;

          for (int j=1 ;j<4;j++)
        {
           System.out.println("Thread 1: " +i+"*"+j+"="+ (i*j));
           Thread.sleep(1000);
        }
     }
     catch(InterruptedException e)
     {
        System.out.println("my thread interrupted");
     }
   }
}
class thread3 extends Thread{
      @Override
    public void run(){

    try
     {
        int i=3 ;

          for (int j=1 ;j<4;j++)
        {
           System.out.println("Thread 2: " +i+"*"+j+"="+ (i*j));
           Thread.sleep(1000);
        }
     }
     catch(InterruptedException e)
     {
        System.out.println("my thread interrupted");
     }
   }
}

class Main {
  public static void main(String[] args) {
    thread1 t1=new thread1();  
    thread2 t2=new thread2(); 
    thread3 t3=new thread3();  

       t1.start();
       t2.start();
       t3.start();
  }
}

Capture222

aidqayyum commented 4 years ago
public class one {

    public static void main(String[] args) throws InterruptedException {

        final Thread mulof2 = new Thread(){
            public void run() {
                for (int i = 1; i <= 3; i++) {
                    int n = 2;
                    int result = n * i;
                    System.out.println(Thread.currentThread().getName()+" 2 * "+i +" = "+result);
                }
            }
        };
        Thread mulof3 = new Thread(){
            public void run() {
                for (int i = 1; i <= 3; i++) {
                    int n = 3;
                    int result = n * i;
                    System.out.println(Thread.currentThread().getName()+" 3 * "+i +" = "+result);
                }
            }
        };

        Thread mulof1 = new Thread(){
            public void run() {
                for (int i = 1; i <= 3; i++) {
                    int n = 1;
                    int result = n * i;
                    System.out.println(Thread.currentThread().getName()+" 1 * "+i +" = "+result);
                }
            }
        };
        mulof2.start();
        mulof3.start();
        mulof1.start();

    }

}

exclass

sufyankamal commented 4 years ago

class Thread1 extends Thread {

@Override
public void run() {
    try {
        for (int i = 1; i <=3; i++) {
            int x = i*1;
            System.out.println("Thread-0:"+i+"*1="+x);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

class Thread2 extends Thread {

@Override
public void run() {
    try {
        for (int i = 1; i <=3; i++) {
            int x = i*2;
            System.out.println("Thread-1:"+i+"*2="+x);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

class Thread3 extends Thread {

@Override
public void run() {
    try {
        for (int i = 1; i <=3; i++) {
            int x = i*3;

            System.out.println("Thread-2:"+i+"*3="+x);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

public class Exercise {

public static void main(String[] args) {
    Thread1 t1 = new Thread1();
    Thread2 t2= new Thread2();
    Thread3 t3= new Thread3();
    t1.start();
    t2.start();
    t3.start();
}

} Untitled

Gv3N commented 4 years ago

Main Class

package com.baktajivan;

public class Main extends Thread{
    public static  void main(String [] args) throws InterruptedException {
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        Thread3 t3 = new Thread3();

        t1.start();
        t2.start();
        t3.start();
    }
}

Thread Class

package com.baktajivan;
//Thread.currentThread().getName() to get thread name

public class Thread1 extends Thread {
    private int total = 0;
    private int i = 0; //for i//thread no
    public void run() {
        try{
            for (int j =1;j<4;j++){
                total = (i*j);
                System.out.println(Thread.currentThread().getName()+": "+i+" * "+j+" = "+total);
            }//multi algo

        } catch (Exception e) {
            e.printStackTrace();
        }
    }//run
}//end class

class Thread2 extends Thread {
    private int total = 0;
    private int i = 1;//for i//thread no
    public void run() {

        try{
            sleep(100);
            for (int j =1;j<4;j++){
                total = (i*j);
                System.out.println(Thread.currentThread().getName()+": "+i+" * "+j+" = "+total);
            }//multi algo

        } catch (Exception e) {
            e.printStackTrace();
        }
    }//run
}//end class

class Thread3 extends Thread {
    private int total = 0;
    private int i = 2;//for i//thread no
    public void run() {
        try{
            sleep(200);
            for (int j =1;j<4;j++){
                total = (i*j);
                System.out.println(Thread.currentThread().getName()+": "+i+" * "+j+" = "+total);
            }//multi algo

        } catch (Exception e) {
            e.printStackTrace();
        }
    }//run
}//end class

Output image

chinsfuh commented 4 years ago
class Thread1 extends Thread
{
        public void run() {
            for (int i = 1; i <= 3; i++) {

                final int n = 1;
                System.out.println("Thread-0:" + n + "*" + i + "=" + (n * i));

            }
        }

        {
            try {
                Thread.sleep(400);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

class Thread2 extends Thread
{
    public void run() {
        for (int i = 1; i <= 3; i++) {

            final int n = 2;
            System.out.println("Thread-1:" + n + "*" + i + "=" + (n * i));

        }
    }

    {
        try {
            Thread.sleep(400);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

class Thread3 extends Thread
{
    public void run() {
        for (int i = 1; i <= 3; i++) {

            final int n = 3;
            System.out.println("Thread-2:" + n + "*" + i + "=" + (n * i));

        }
    }

    {
        try {
            Thread.sleep(400);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

public class MultipleThread extends Thread
{
    public static void main(String args[])
    {

        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        Thread3 t3 = new Thread3();
        t1.start();
        t2.start();
        t3.start();
    }
}

image

muhdhariz commented 4 years ago
package Issue10;

public class Main {
    static int i = 3;
    public static void main(String[] args) {
        for (int l = 0; l < i; l++) {
            Thread t1 = new multiplicationThread();
            t1.start();
        }
    }
}
class multiplicationThread extends Thread{
    private static int n;

    public void run() {
        printTable(n++);
    }

    private void printTable(int n) {
        for (int i = 1; i <= Main.i; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + (n+1) + " * " + i + " = " + (n+1) * i);
        }
    }

}

image

SilentHlive commented 4 years ago
package com.company;

public class Multiplication_Table extends Thread {
    public static void main(String[] args) {
        threadrun ts = new threadrun();
        Thread mt = new Thread(ts);
        Thread mt1 = new Thread(ts);
        Thread mt2 = new Thread(ts);

        mt.setName("0");
        mt1.setName("1");
        mt2.setName("2");

        mt.start();
        mt1.start();
        mt2.start();
    }

}

class threadrun extends Thread {
    int j=1;
    public void run() {
        int n = count();
        for (int i = 1; i <= 3; i++) {
                System.out.println("Thread-" + Thread.currentThread( ).getName( ) + ": " + n+ " * " + i + " = " + (i * n));
               }
    }

    int count(){
        return j++;
    }
}

multiple

yyjmax commented 4 years ago
import java.util.Random;

public class test01 {
    public static void main(String[]args){
        for(int j=0;j<=3;j++){
            RunThread rt = new RunThread(j);
rt.run();
        }
    }
}

class get_name{
   public int getName(){
        Random rd = new Random();
        int num = rd.nextInt(4);
        while (num<1){
            num = rd.nextInt(3);
        }
        return num;
    }

}

class RunThread extends Thread{
    int num;
    public RunThread(int num){
        this.num = num;
    }
public void run(){
    synchronized (this){
        for(int i = 1;i<=3;i++){
            System.out.println(getName()+":"+num+"*"+i+"="+(num*i));
        }
    }

}
}

ThreadTest matric:243147

AhmedBawazir2020 commented 4 years ago

Ahmed Bawazir 249879

package issue10;

import static issue10.Issue10.lock; import static java.lang.Thread.sleep; import java.util.concurrent.locks.ReentrantLock;

/*

}

class Thread1 extends Thread {

private int x=1;
private int sum =0;
public void run() {

    try {
        for (int i = 1; i < 4; i++) {
         sum =(x*i);   
            lock.lock();
            System.out.println(Thread.currentThread().getName() + ":" + 1 + "" + " * " + i + " = " + sum);
            lock.unlock();
        }
        System.out.println();
    } catch (Exception e) {

    }

}

}

class Thread2 extends Thread {

private int x=2;
private int sum =0;
public void run() {

    try {
        for (int i = 1; i < 4; i++) {
             sum =(x*i);
            lock.lock();
            System.out.println(Thread.currentThread().getName() + ":" + 2 + "" + " * " + i + " = " + sum);
            lock.unlock();
        }
        System.out.println();
    } catch (Exception e) {

    }

}

}

class Thread3 extends Thread {

private int x=3;
private int sum =0;
public void run() {

    try {
        for (int i = 1; i < 4; i++) {
             sum =(x*i);
            lock.lock();
            System.out.println(Thread.currentThread().getName() + ":" + 3 + "" + " * " + i + " = " + sum);
            lock.unlock();
        }
        System.out.println();
    } catch (Exception e) {

    }

}

}

Issue10

sonyhana7 commented 4 years ago
public class mains {

        static int i = 3;
        public static void main(String[] args) {
            for (int l = 0; l < i; l++) {
                Thread t1 = new multiply();
                t1.start();
            }
        }
    }

public class multiply extends Thread {

    private static int n;

    public void run() {
        printTable(n++);
    }

    private void printTable(int n) {
        for (int i = 1; i <= mains.i; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + (n+1) + " * " + i + " = " + (n+1) * i);
        }
    }

}

image

FatihahFauzi commented 4 years ago

public class multitable {

    public static void main(String []args){
        for(int i=1;i<=3;i++) {
            Runnable runnable = new myThread(i);
            Thread thread = new Thread(runnable);
            thread.setName("Thread-"+(i-1));
            thread.start();
        }
    }
}

class myThread implements Runnable {
    private int threadNo;

    myThread(int threadNo){
        this.threadNo = threadNo;
    }

    public void run(){
        for(int i=1;i<=3;i++){
            System.out.println(Thread.currentThread().getName()+": "+threadNo+" * "+i+" = "+(threadNo*i));
        }
    }
}

Capture

ychian234 commented 4 years ago
public class Multiplication {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i <= 3; i++) {
            Thread11 mm = new Thread11(i);
            mm.start();
        }
    }
}
class Thread11 extends Thread {
    int num;
    Thread11(int i) {
        this.num = i;
    }
    @Override
    public void run() {
        synchronized (this) {
            for (int i = 1; i <= 3; i++) {
                System.out.println("Thread-" + (num - 1) + ": " + num + " * " + i + " = " + (num * i));
            }
        }
    }
}

multiplication