[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
selvam kumar wrote:how to understand this concept clearly..................???
coming saturday i have ocpjp exam ... so please.....
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
selvam kumar wrote:okay friends...
thread concepts makes lot of confusions....
particularly the multiple threads with synchronize and without synchronize methods or blocks...
how to understand this concept clearly..................???
selvam kumar wrote:hello friends....,
by running following code...
public class threads1 {
int x=0;
public class runner implements Runnable
{
public void run()
{
int current=0;
for(int i=0;i<4;i++)
{
current=x;
System.out.println(current+" ");
x=current+2;
}
}
}
public static void main(String args[])
{
new threads1().go();
}
public void go()
{
Runnable r1=new runner();
new Thread(r1).start();
new Thread(r1).start();
}
}
and the output i got:
0
2
4
4
6
8
10
6
anyone please explain the concept ....
Amey Ambulgekar wrote:
it's because when we create any thread and when we call start() then it will go to run() method ok.. then after that, each thread is sharing for() loop hence output will be
0
0
2
2
4
4
6
6