• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

creating user defined threads

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know how to create threads but I need to create threads based on user input, i.e., if you mention 5 then 5 threads have to be generated. each thread has different tasks to do.
 
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

Mohan Mehra wrote:I know how to create threads but I need to create threads based on user input, i.e., if you mention 5 then 5 threads have to be generated.



So use a loop.

each thread has different tasks to do.



Then you must have some logic for deciding which task to assign to each thread. Nobody here can tell you how to do that, since it depends on your requirements, which you haven't specified.
 
Mohan Mehra
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
each thread generated will independently generate a random integer as an upper limit, and then computes and prints the no. of primes less than or equal to that upper limit .

simple program to create thread:
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simply... replace this code ...

Mohan Mehra wrote:



with something that does this...

Mohan Mehra wrote:independently generate a random integer as an upper limit, and then computes and prints the no. of primes less than or equal to that upper limit .



Also, modify this code ...

Mohan Mehra wrote:



to start many threads by adding ...

Jeff Verdegan wrote:
So use a loop.



whose range is controlled or obtained ...

Mohan Mehra wrote:if you mention 5 then 5 threads have to be generated.



from some input source.


Also ... Since most, if not all of this problem is not thread related, it may be best to implement and test the components without threads first. So, do you have code that generates a random number? that gets input from a user? that generate prime numbers?

Henry
 
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Henry Wong : Nicely put...

@Mohan Mehra : as Henry rightly put, address it step by step basis. Not to be rude but a word of advice: These forums are here to help you understand things and code better. That means a fair amount of home work by original poster is expected. Bottom line - please do not expect people to code it for you.
 
Mohan Mehra
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
done it. here the code

 
Why is the word "abbreviation" so long? And this ad is so short?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic