Results 1 to 6 of 6
  1. #1
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    9

    Default Enforcing different threads use different cores

    Hi!

    So I'm in the (planning) process of writing a program to read/write a fair amount of data to different HDDs. I figured I'd speed things up a bit by using different threads for each HDD (at least I'm assuming it'll speed things up, since it'd allow for multiple read/write operations at a time, rather than doing them one by one). Problem is, I can't find any way to lock a thread to a specific core... Is there a way to do that, or would it be better to just do the operations one at a time rather than taking the complicated path using threads (which will, obviously, require a fair amount of checks)?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Enforcing different threads use different cores

    Why do you need to "lock a thread to a core"?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    9

    Default Re: Enforcing different threads use different cores

    Well, in this specific example, if two threads that both perform IO run on the same core, only one IO operation would take place at a time. By using two cores, I can use two IO operations at a time (assuming, of course, that they target different physical HDDs). Granted, the "twice the IO" is just a thought I'm having; I'm not sure if it'll even end up being the case.

    I've also had the same issue in another piece of code I wrote, which was extremely processor intensive. Unfortunately though, despite using threads, most of the time the threads ended up on the same core, meaning only 50% of my CPU was actually used. On occasion the threads would end up on different cores making it 100%, but it was far from common enough to be even remotely reliable.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Enforcing different threads use different cores

    I do not believe you can do that. In fact, it would be very un-Java like to provide that level of control over the processor. I would suspect it is done between some coordination between the JVM and the host OS. In fact, the JVM may be hostage to what the OS provides. But this is all speculation on my part.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Enforcing different threads use different cores

    Long story short, this is completely up to the OS, not Java.

    However, Java 7 did introduce the Fork/Join framework if you're especially curious: Fork/Join (The Java™ Tutorials > Essential Classes > Concurrency)
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Enforcing different threads use different cores

    It is indeed one of those things you can't really control through Java. The best you can do is use Java 8 and its features which is very much optimized to let you run code in parallel and the JVM can do its best to work with the OS to offload the work as best as possible.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How to find no. of cores(Processors) by using java???
    By piyushr999 in forum Advanced Java
    Replies: 1
    Last Post: 01-17-2013, 05:11 PM
  2. Threads and cores
    By aburnett397 in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-07-2011, 03:36 AM
  3. Replies: 0
    Last Post: 03-03-2011, 04:58 PM
  4. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 03:48 PM
  5. How to band threads with cup cores?
    By young in forum Threads and Synchronization
    Replies: 4
    Last Post: 04-06-2010, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •