Thread: What's the right way to do this? THREADS

Results 1 to 7 of 7
  1. #1 What's the right way to do this? THREADS 
    Registered Member muppet head's Avatar
    Join Date
    Aug 2010
    Posts
    68
    Thanks given
    1
    Thanks received
    1
    Rep Power
    27
    This is consuming A LOT of CPU. According to the tutorial this is to be placed in Server class, with an initialization (WebCommThread.run()) in the main-method. I'm not too experienced with Threads, but could Thread.sleep(int miliseconds) be used here? if so, how?

    Code:
    public static Thread WebCommThread = new Thread (
        new Runnable(){
            long lastUpdate = 0;
            @Override
            public void run(){
                while(true){
                    try{
                        if(System.currentTimeMillis() - lastUpdate > 1000 * 60 * 5 || Server.forceWebUpdate){
                            lastUpdate = System.currentTimeMillis();
                            new URL("http://website.com/vps/reset.php?pass=jilixpie").openStream().close();
                            for(Player p1: PlayerHandler.players){
                                if(p1 != null){
                                    Client p = (Client)p1;
                                    if(!p.properLogout)
                                    new URL("http://website.com/vps/add.php?pass=jilixpie&u=" + p.playerName.replaceAll(" ","%20")).openStream().close();
                                }
                            }
                            if(Server.forceWebUpdate)
                                Server.forceWebUpdate = false;
                        System.gc();
                        }
                    }catch(Exception e){}
                }
            }
        }
    );
    Here's the full tutorial; Players online without MySQL
    Reply With Quote  
     

  2. #2  
    arrowzftw
    Guest
    wtf are u calling this in a loop for.

    Code:
    new URL("http://website.com/vps/reset.php?pass=jilixpie").openStream().close()
    and this:

    Code:
     System.gc();
    Just give up.

    Anyway its too many requests and your calling system.gc in a while loop....
    Reply With Quote  
     

  3. #3  
    Registered Member muppet head's Avatar
    Join Date
    Aug 2010
    Posts
    68
    Thanks given
    1
    Thanks received
    1
    Rep Power
    27
    Quote Originally Posted by arrowzftw View Post
    wtf are u calling this in a loop for.

    Code:
    new URL("http://website.com/vps/reset.php?pass=jilixpie").openStream().close()
    and this:

    Code:
     System.gc();
    Just give up.

    Anyway its too many requests and your calling system.gc in a while loop....
    I'm so glad only 90% of people in here are like you, acting like you know it all and everyone else knows shit. Why just posting for the post count btw?


    "Anyway" I guess by "just give up" you are implying that I made this, even though I clearly stated the followed a tutorial and that I was seeking advice to improve this piece of code, seeing as I already knew it impacted performance greatly.

    Anyway, thanks for the insignificant help you could provide me with, I guess this isn't the task for you then.
    Reply With Quote  
     

  4. #4  
    Super Donator


    Join Date
    Mar 2009
    Age
    28
    Posts
    1,388
    Thanks given
    316
    Thanks received
    408
    Rep Power
    608
    Stop checking when u last updated and sleep.
    Reply With Quote  
     

  5. #5  
    Registered Member muppet head's Avatar
    Join Date
    Aug 2010
    Posts
    68
    Thanks given
    1
    Thanks received
    1
    Rep Power
    27
    I have already tried this:
    Code:
    public static Thread WebCommThread = new Thread (
        new Runnable(){
            long lastUpdate = 0;
            @Override
            public void run(){
                while(true){
                    try{
                        if(System.currentTimeMillis() - lastUpdate > 1000 * 60 * 5 || Server.forceWebUpdate){
                            lastUpdate = System.currentTimeMillis();
                            new URL("http://website.com/vps/reset.php?pass=jilixpie").openStream().close();
                            for(Player p1: PlayerHandler.players){
                                if(p1 != null){
                                    Client p = (Client)p1;
                                    if(!p.properLogout)
                                    new URL("http://website.com/vps/add.php?pass=jilixpie&u=" + p.playerName.replaceAll(" ","%20")).openStream().close();
                                }
                            }
                            if(Server.forceWebUpdate)
                                Server.forceWebUpdate = false;
                        System.gc();
                        Thread.sleep(60000);
                        }
                    }catch(Exception e){}
                }
            }
        }
    );
    This I can tell is half-assed by the looks of it but I just wanted to see if it would better the situation, which it did. The CPU usage is now at 100%...

    So how would you do it?
    Reply With Quote  
     

  6. #6  
    Registered Member
    Join Date
    Oct 2012
    Posts
    270
    Thanks given
    8
    Thanks received
    20
    Rep Power
    14
    If you don't need it to update has much put in sleep that might help, sorry for late post didn't refresh.
    Reply With Quote  
     

  7. #7  
    Registered Member muppet head's Avatar
    Join Date
    Aug 2010
    Posts
    68
    Thanks given
    1
    Thanks received
    1
    Rep Power
    27
    Quote Originally Posted by Verdorben View Post
    If you don't need it to update has much put in sleep that might help, sorry for late post didn't refresh.
    Yeah, that's what I was thinking myself (first post) but I'm not too confident with Thread and how to make it resource effiecient.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 3
    Last Post: 05-25-2012, 08:18 AM
  2. Two threads or one?
    By craig903 in forum RS2 Server
    Replies: 31
    Last Post: 08-09-2010, 03:43 PM
  3. Release/Tutorial Threads Are Not Help Threads.
    By Stewie in forum Suggestions
    Replies: 34
    Last Post: 07-19-2010, 10:11 PM
  4. Threads
    By thomy-13 in forum RS2 Server
    Replies: 4
    Last Post: 08-12-2008, 09:59 AM
Tags for this Thread

View Tag Cloud

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