I need a little help to create a program that prints ABC, always in that order, in Java, using Threads, in which each thread should be a letter.
I am new to Threads in Java.
The requirement is that I must use join(), and the main program can only have the object creation and start of the threads.
Cannot use join inside the main program. Cannot use sleep. Also cannot use join with parameters, like join(1000), it should be join() only.
This is what I have done so far:
public class Write extends Thread
{
private String letter;
public Write(String letter)
{
this.letter = letter;
}
@Override
public void run ()
{
try
{
if ( letter == "A")
{
this.join();
System.out.print(letter);
}
else if ( letter == "B")
{
this.join();
System.out.print(letter);
}
else if ( letter == "C")
{
System.out.print(letter);
}
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}
public class TestWrite {
public static void main(String[] args) {
Write letter1 = new Write("A");
Write letter2 = new Write("B");
Write letter3 = new Write("C");
letter1.start();
letter2.start();
letter3.start();
}
}
The code above wouldn't work, it prints only C and hangs. Could someone please give me some guidance on how to make this work ?
Thanks, Filipe