Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading is not working correctly?

Doing a threading problem and I am not sure if this is how its supposed to be or if I coded incorrectly. From what I understand, threading should have multiple methods going at the same time and because of this they should be intertwined. My code is supposed to take a single char and repeat 1000 times but instead of having different variations of the two letters it goes "a" a thousand times, then "b" a thousand times. What is my issue?

Main Method

import java.util.*;
public class MainThread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner answer = new Scanner(System.in);

        System.out.println("Give me a single character: ");
        char h = answer.next().charAt(0);
        System.out.println("Give me another single character: ");
        char a = answer.next().charAt(0);   

        MyThread t1 = new MyThread(h);
        MyThread t2 = new MyThread(a);

        t1.start(h);
        t2.start(a);        

        answer.close(); 
    }
}

my Threading class

import java.util.*;
public class MyThread extends Thread{

    Scanner answer = new Scanner(System.in);

    public MyThread(char x) {
        // TODO Auto-generated constructor stub
    }


    public void Stored(char x){
        System.out.println("Type a single letter here: ");      
    }


    //modified run method 
    public void start(char x){

        for(int i = 0; i < 1000; i++){
            System.out.print(x);
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {              
                e.printStackTrace();
            }
            Thread.yield();
        }
    }   
}
like image 219
spotTop Avatar asked Apr 17 '26 20:04

spotTop


2 Answers

What you have done is NOT multithreading, rather you have called the start method sequentially, i.e., in order to run the multiple threads parallelly, you need to override the run() method in your MyThread class.

The important point is that run() method will be called by JVM automatically when you start the Thread, and the code inside run() will be executed in parallel with the main/other threads, so override run() inside MyThread class as shown below:

class MyThread extends Thread {

    private char x;

    public MyThread(char x) {
        this.x= x;
    }

    // Add run() method
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.print(x);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            Thread.yield();
        }
    }
}

MainThread class:

public class MainThread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner answer = new Scanner(System.in);

        System.out.println("Give me a single character: ");
        char h = answer.next().charAt(0);
        System.out.println("Give me another single character: ");
        char a = answer.next().charAt(0);   

        MyThread t1 = new MyThread(h);
        MyThread t2 = new MyThread(a);

        t1.start();//this calls run() of t1 automatically
        t2.start();//this calls run() of t2 automatically        

        answer.close(); 
    }
}

I suggest you have a look here for basic understanding on how to create and start the Thread and how multi-threading works.

like image 64
developer Avatar answered Apr 19 '26 09:04

developer


In order to let the threads run in parallel, the run method needs to be implemented instead of start.

See the JavaDoc for Thread.start():

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

like image 29
ldz Avatar answered Apr 19 '26 10:04

ldz