• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Threads  RSS feed

 
mahant Awati
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aim : display a serial number using two thread Say (odd numbers)t1=1,3,5,7....99, and (even no) t2=2,4,6,8......100
How to resolve this problem ?

output will be: 1
2
3
:
100

Thanks & Regards
Mahantesh
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried and where are you having problems ?
You may want to read HowToAskQuestionsOnJavaRanch - especially the ShowSomeEffort and NotACodeMill parts.

The first thing we need to know though is - why do you want to use threads ? Threads are not designed to do this sort of thing.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just guessing. I suspect this is some kind of exercise to learn thread synchronization.
There would be 2 threads displaying numbers alternately.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you know about race conditions? If that is what PP thinks, an exercise then you have a race condition. You have to allow the threads access to some object which produces those numbers, and make sure they get there in turn. I presume you have read the Java Tutorials section about threads.
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To achieve that, both Threads must wait until the other is done with their job. You can use wait/notify mechanism to implement that.

Here is a example:



More about Threads here
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Arthur Vinicius Rebelo
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sheriff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!