简体   繁体   English

TImer在Java中无法正常工作

[英]TImer is not working properly in java

here is my Timer scheduler 这是我的Timer调度程序

   timer.schedule(new SuperOne(),new Date(),10);

In SuperOne class in run method i am calling synchronized method By using above code That task is only working once. run方法的SuperOne类中,我通过使用上述代码调用synchronized方法,该任务仅工作一次。

My requirement is that It has to call the synchronized method for every minute(60 seconds). 我的要求是它必须每分钟(60秒)调用一次synchronized方法。 Here timer scheduler is not working what I expect ... Is it because I am running synchronized method ? 在这里计时器调度程序无法正常工作……是因为我正在运行synchronized方法吗?

Please help me In this 请帮我

* EDIT: Its working for first time (calling one time ) and not calling after 10 milliseconds * * 编辑:它是第一次工作(调用一次),并且在10毫秒后不再调用*

This is the code has to 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 is  -- >"+ sWatch.toString());
    }else
    {
        System.exit(0);
    }



    }catch(Exception e)
    {
        e.printStackTrace();
        System.exit(0);
    }finally
    {
        sWatch.stop();
    }
}

Class which extends TimerTask 扩展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 , which means that only one thread can every access that function at a time (which means realistically only one task can ever be active at a time). 您已使run synchronized ,这意味着一次只能有一个线程可以访问该函数(实际上这意味着一次只能有一个任务处于活动状态)。 I do not believe this is what you intended to do. 我不认为这是您打算要做的。 I imagine what you intended to do was make what run calls during its execution synchronized , so that only one of the running tasks can call it at a time to ensure consistency. 我想您想做的是使run过程中的run调用synchronized ,以便每次运行的任务一次只能调用一次以确保一致性。

A lot of people get tripped up on exactly what synchronized does, because it doesn't do what a lot of people expect it to (although it does do what its name implies if you think about common use cases for it). 很多人完全了解synchronized所做的事情,因为synchronized并不能满足很多人的期望(尽管考虑到通用用例,它确实可以实现其名称所暗示的功能)。 See the Java synchronized tutorial for more. 有关更多信息,请参见Java 同步教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM