File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java: Thread

 
selvam kumar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
selvam kumar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic