• 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 7 multithreading.

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hello All,

I have an application task to design and code and using Java 7. Its been long time that I did code using Multi thread, didn’t have much idea on Java 7 API.

I have a scenario where N tasks returned from DB .. each task will have again N tasks to complete.

Ex:

In this scenarios I want to create 5 threads ( 1 .. 5) and each thread has 10 ( 1 .. 10) tasks to complete, need to create 10 threads. It is like multi thread in multithread. Can someone give me examples to start coding this?



1 -- thread
--------------
1 2 3 4 5 .. 10


2 – thread
-------------
1 2 3 4 5 .. 10


3 – thread
-------------
1 2 3 4 5 .. 10


4 – thread
-------------
1 2 3 4 5 .. 10



Appreciate your help.

thanks
 
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Sireesh,
Please check out the java concurrency APIs. At some point (maybe it was Java 7), there was a release of some more advanced multithreading code. There was a book by Brian Goetz called "Java Concurrency in Practice", which has a lot of good stuff about this. I saw him present at No Fluff Just Stuff one year.

Now, your particular case: I do that sort of thing a lot nowadays using an Executor (or ExecutorService). You would use the Executors class to request one of these things for your use. Then you would make Runnables (or newer Callables), and you would submit the Callables to your Executor.

When these parallel-running tasks are complete, you will have to decide whether you want some other code to proceed after they are all done, and if so, there are steps you can take. The instructions on how to do all this are here.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was Java 5 when the new concurrency utilities were added to Java. That's 10 years ago, so for requirements like Sireesh posted there's no reason to use anything else.

The API documentation is actually a pretty good resource for understanding executors and so on, but to complement that here's a link to the Oracle tutorial trail.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic