• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java: Thread

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ....
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic