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();
}
}
}
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.
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
runmethod of this thread.The result is that two threads are running concurrently: the current thread (which returns from the call to the
startmethod) and the other thread (which executes itsrunmethod).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With