aspose file tools*
The moose likes Threads and Synchronization and the fly likes Java: Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Java: Thread" Watch "Java: Thread" New topic
Author

Java: Thread

selvam kumar
Greenhorn

Joined: Nov 12, 2011
Posts: 17
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 ....
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

OK, tell us first what you expect and what you got in output?
If you're wondering why code doesn't' print values in order, then I'll give you one hint: How do you avoid 2 threads accessing same instance field ?

And pl use code tags,


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Because you have no synchronization, the second thread can run multiple loop iterations between the call to current = x and x = current + 2 of the first thread. This is why you get the 6 at the end.

I'll move this thread to our Threads forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
selvam kumar
Greenhorn

Joined: Nov 12, 2011
Posts: 17
i expect these two outputs..
0
2
4
6
8
10
12
14
or else
0
0
2
4
6
2
4
6
but i got the output :
0
2
4
4
6
8
10
6

please explain clearly...
i am slightly weak in this concepts....
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
As told by others, there is no synchronization. so the following code may help you


output:
0
2
4
6
8
10
12
14

selvam kumar
Greenhorn

Joined: Nov 12, 2011
Posts: 17
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..................???
coming saturday i have ocpjp exam ... so please.....
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

selvam kumar wrote:how to understand this concept clearly..................???
coming saturday i have ocpjp exam ... so please.....

If you are afraid about the guessing the correct output sequence for Thread related problem, then no worry.. Its always hard to predict the output/thread execution sequence if more than one thread involves (exception may be if there is some kind of sequencing done, like using thread.join())

So focus on the question which cover the Thread life cycle, priority, various methods and synchronization and do not waste time finding the correct output sequence for thread related code.

Best luck for exam
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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..................???


What are you still confused about? It's hard to help you if you don't ask a more precise question.

Also, please make an effort to write correct English, including proper capitalization and a single punctuation mark to end a sentence. Doing so will make your posts easier to understand.
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
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 ....



Hello selvam kumar

i read your Q and and i run your code and i found different output .

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

and hope you will read this post and also... lets hope you have given exam with full of confidence.. and if you gave this exam then could you please tell me what kind of Q you face in exam and how you prepare

for that and how you study JAVA and which books you have preferred.. because i am preparing for JAVA ....

Thank you.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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


No, the output will not necessarily look like that. The OP has already stated he got different output. With the code provided, it is impossible to predict exactly what the output will be, and it can be different on different hosts or on subsequent runs on the same host.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java: Thread