Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 7 multithreading.  RSS feed

 
Sireesh Ganagam
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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
 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!