multithreading - TImer is not working properly in java -


here timer scheduler

   timer.schedule(new superone(),new date(),10); 

in superone class in runmethod calling synchronized method using above code task working once.

my requirement has call synchronized method every minute(60 seconds). here timer scheduler not working expect ... because running synchronized method ?

please me in this

*edit: working first time (calling 1 time ) , not calling after 10 milliseconds *

this code has run

    private boolean proxyenabled=false; public synchronized void statuschecker() {     stopwatch swatch = new stopwatch();      resourcebundle resource = resourcebundle.getbundle("resources.application");     system.out.println(resource.getstring("url"));      try {     url url = new url("https://www.google.com/");     httpurlconnection urlconnection;       if(proxyenabled) {         proxy proxy = new proxy(proxy.type.http, new inetsocketaddress("proxyhost", portnumber));         swatch.start();         urlconnection =(httpurlconnection) url.openconnection(proxy);         system.out.println(urlconnection);     } else {         urlconnection =(httpurlconnection)url.openconnection();         swatch.start();     }       system.out.println(urlconnection.getresponsecode());      in = new bufferedreader(new inputstreamreader(urlconnection.getinputstream()));      if(in!=null)     {         system.out.println("the resopose time  -- >"+ swatch.tostring());     }else     {         system.exit(0);     }        }catch(exception e)     {         e.printstacktrace();         system.exit(0);     }finally     {         swatch.stop();     } } 

class extends timertask

   public class superone extends timertask{    boolean flag = false;  @override  public synchronized void run()  {     // todo auto-generated method stub     try {          system.out.println("*** * ** thread started ** ** *** ");             thread th = thread.currentthread();             system.out.println(th.isalive());         checkserver cs  = new checkserver();         cs.statuschecker();        }     catch(exception e)     {         system.out.println("exception in run iterface "+e);         e.printstacktrace();     }   }  } 

you've made run synchronized, means 1 thread can every access function @ time (which means realistically 1 task can ever active @ time). not believe intended do. imagine intended make run calls during execution synchronized, 1 of running tasks can call @ time ensure consistency.

a lot of people tripped on synchronized does, because doesn't lot of people expect (although name implies if think common use cases it). see java synchronized tutorial more.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -