Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Performance of threads in Java

Posted on 2011-05-12
11
Medium Priority
?
307 Views
Last Modified: 2012-06-22
Hey,

       I was trying to build a parallelized depth first search in Java.  I was trying to figure out the best way to handle the threads.

I read on http://developer.amd.com/Pages/1121200683.aspx that it was faster to have threads alive, rather than shutting them down when we'r done with them.

So, basically... I was wondering if it was possible to create an array of threads, start them from a parent thread when needed, allowing the parent thread to go to sleep, and then let the child threads go to sleep after they are done exploring the edges and wake up the parent thread when  the child threads were done.

The main questions I wasn't sure about was:
1) how do we get the number of active threads?
2) how do we allow the threads to share a common variable?

Could anyone help me out?
0
Comment
Question by:errang
  • 5
  • 4
  • 2
11 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35751329
it is proably dependent on the implmentation of the thraeds on particular system where you are working
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35751332
if you have different threads running in different cores on multicore processor, as in the link you mentioned
it is probably different on non-multicore system and with threads implemented differently
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35751339
I guess you can count them as you create them
And they will share the common variables if they are accessible in the scope, there are many examples
of those in the code for threads
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 35751341
0
 
LVL 92

Expert Comment

by:objects
ID: 35751343
> 2) how do we allow the threads to share a common variable?

pass them each an object containing the data they need to share
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 800 total points
ID: 35751344
and you can have static variable in the class counting the active threads, and bfore the thread goes to sleep it can
change thee number of actuve threads - reduce it by one, then increment when it wakes up
0
 
LVL 92

Expert Comment

by:objects
ID: 35751349
0
 

Author Comment

by:errang
ID: 35751365
>>you can also get the active count from a ThreadGroup

Hm... actually, is there any way to get the id of the threads that are active or free? Or is there a isRunning() method that would tell me if a specific thread in the array of threads is running?
0
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 35751366
the Thread class has methods for checking their status
0
 

Author Comment

by:errang
ID: 35751374
ah, alright, thanks.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35751375
There is a method isAlive(), but is it sleeping is probably more difficult
In any case all these thread things are very much implemmentaiton dependent,
you need to experiment with them, it is often not what is written in API - this is my experience with threads
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question