Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 1 Time critical thread and many low priority threads - time critical being slowed

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 1 Time critical thread and many low priority threads - time critical being slowed

    I'm attempting to create a game with (obviously) a main game loop and also several threads doing work that can be finished 'whenever'; getting new game content ready and such. However, despite setting the secondary threads as Minimum Priority they are still slowing down the main thread. The below is code which exibits the behaviour in the bare minimum code.

    This code runs at below the requested 60 fps (sometimes as low as 21fps) and occassionally freezes the main thread

    If I uncomment the Thread.yield() section it behaves correctly, with the secondary threads using whatever spare processor time is available. However, netbeans produces a warning saying this is basically very unwise. So what is the correct way to get the behaviour I want: 1 thread taking as much processor as it wants with secondary threads taking whatevers left.

    Thanks for any advice

    import java.util.ArrayList;
     
     
    public class Main {
     
        public static void main(String[] args) {
            long count=0;
            int fps=60;
            long startTime;
     
            int noOfTimeWasters=50;
            ArrayList <Subsidurary> timeWasters=new ArrayList <Subsidurary>();
     
            for(int i=0;i<noOfTimeWasters;i++){
                Subsidurary newSub=new Subsidurary();
                newSub.t.start();
                timeWasters.add(newSub);
            }
     
     
            startTime=System.nanoTime();
     
            while(true){
                count++;
                long time=System.nanoTime();
                try {
                    Thread.sleep(1000/fps); //I have used Thread.sleep to simulate a jmonkeyengine simpleUpdate call
                } catch (InterruptedException ex) {
     
                }
                double expendedSeconds=((time-startTime)/(1000d*1000d*1000d));
                int actualFPS=(int)(count/expendedSeconds);
     
                System.out.println("" + actualFPS);
            }
        }
    }

    public class Subsidurary implements Runnable{
     
        Thread t;
     
        public Subsidurary(){
            t=new Thread(this,"Subsidurary");
            t.setPriority(Thread.MIN_PRIORITY);
     
        }
     
     
        @Override
        public void run() {
     
            while(true){
                for(int i=0;i<10000000;i++){
                    double b=Math.sqrt(i);
                    b=Math.sin(b);
                    b=Math.cos(b);
                    b=Math.pow(b, 2);
                    /*if (i%1000==0){
                        Thread.yield();
                    }*/
                }
                System.out.println("SubCompleted");
            }
     
        }
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 1 Time critical thread and many low priority threads - time critical being slowed

    Sounds like you could benefit a lot from Executors and Thread pools.

    Also, you're starting 50 threads. The ideal number usually is closer to the number of physical cores your computer has (say no more than 2x the number of available cores). There are exceptions, but these applications are more focused on the other benefits of multi-threading rather than computational performance.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    RichTea (October 19th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 1 Time critical thread and many low priority threads - time critical being slowed

    Thanks; that thread pool sounds very like something I was considering creating manually to manage the problem so thank you for pointing me to it

Similar Threads

  1. Threads priority: low priority run faster than high priority
    By leonixyz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2011, 04:33 PM
  2. Thread for a given time window
    By zu1u in forum Threads
    Replies: 1
    Last Post: November 9th, 2010, 12:57 PM
  3. Replies: 4
    Last Post: July 21st, 2010, 04:07 PM
  4. set time limit on running of a thread
    By z.zojaji in forum Threads
    Replies: 3
    Last Post: July 10th, 2010, 10:57 AM
  5. Exercises - CRITICAL HELP NEEDED
    By Maximillian in forum Java Theory & Questions
    Replies: 5
    Last Post: November 17th, 2009, 05:55 PM

Tags for this Thread