I have a problem with understanding this code , My question is why the two threads does not interfere with each other during executing the statements in run
method ?
I mean that always a thead do all statements then come second thread to do statements.
it is impossible that first thread do portion of statements then second thread do portion of statements then first thread continue its task .............
note : I know that the two threads uses different OutputStream
object
Thread code here
class Printer extends Thread
{
private String ThreadName;
public Printer(String name)
{
this.ThreadName=name;
}
public void run()
{
PrintStream out=new PrintStream(System.out);
out.println(this.ThreadName+" : a");
out.println(this.ThreadName+" : b");
out.println(this.ThreadName+" : c");
out.println(this.ThreadName+" : d");
out.println(this.ThreadName+" : e");
out.println(this.ThreadName+" : f");
out.println(this.ThreadName+" : g");
out.println(this.ThreadName+" : h");
out.println(this.ThreadName+" : i");
out.println(this.ThreadName+" : j");
out.println(this.ThreadName+" : k");
}
}
Entry Code:
class Main
{
public static void main(String[] args)
{
Thread t1 = new Printer("thread 1");
Thread t2 = new Printer("thread 2");
t1.start();
t2.start();
}
}
try to replace System.out
with out
and compare the results then you will know exactly what am I asking for