Is TimerTask running in a new thread

Is it possible to assume that the code in run will be executed in a new thread, or should I use AsyncTask ?

 Timer myTimer = new Timer(); //   final Handler uiHandler = new Handler(); myTimer.schedule(new TimerTask() { //   @Override public void run() { uiHandler.post(new Runnable() { @Override public void run() { } }); } ; }, 0L, 10L * 1000); //  - 10000 , 0    . 

UPDATED

I got an error in this code:

 Timer myTimer = new Timer(); final Handler uiHandler = new Handler(); myTimer.schedule(new TimerTask() { @Override public void run() { while (songRefreshing) { uiHandler.post(new Runnable() { @Override public void run() { try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = null; response = httpclient.execute(new HttpGet(Const.php_url)); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); String responseString = out.toString(); if (app.getCurSong() == null || app.getCurSong().intern() != responseString.intern()) { app.setCurSong(responseString); song_name.setText(app.getCurSong()); Log.d(LOG_TAG, "refreshCurSung - " + responseString); } } else { response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (IOException e) { e.printStackTrace(); Log.d(LOG_TAG, e.toString()); } } }); } } ; }, 0L, 10L * 1000); // 10s interval 

Error:

 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178) at java.net.InetAddress.lookupHostByName(InetAddress.java:394) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245) at java.net.InetAddress.getAllByName(InetAddress.java:220) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:590) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:510) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:488) 

Does this mean that TimerTask is not executing in a new thread?

+4
source share
1 answer

To answer your question directly, the quote is taken from here :

Class overview

Timers schedule one-time or recurring tasks to complete. prefer ScheduledThreadPoolExecutor for new code.

Each timer has one thread through which tasks are performed sequentially. When this thread is busy executing tasks, ongoing tasks may be delayed.

One shot is planned to be launched in absolute time or after a delay.

Repetitive tasks are assigned either a fixed period or a fixed Speed:

When the fixed period is executed by default, each subsequent task launch is scheduled relative to the start time of the previous run, so the two runs never work closer to each other in time than the specified period. When performing a fixed speed, the start time of each subsequent task launch is scheduled without taking into account when the previous start. This can lead to a series of running starts (one starts immediately after the other) if delays do not allow the timer to start tasks on time. When the timer is no longer needed, users must call cancel (), which frees the timer thread and other resources. Timers that are not explicitly canceled can contain resources unlimitedly.

This class provides no guarantees regarding real-time task scheduling. Multiple threads can share the same timer without synchronization.

So this is the thread.

UPDATE (after updating the Question):

The current implementation may contain many flaws, so I would recommend that you put the code from Runnable into AsyncTask (and put the whole bunch of code in the doInBackground method). There you can easily manage it.

Also, I think that @Overriding run() two times inside the other can lead you to a dead end or something else. Since TimerTask is actually a thread, I don’t think you need a separate Runnable inside.

Remove the Runnable implementation to run and try to run TimerTask with the HttpClient inside it (without Runnable ). If you fail to do this, enter the code (as suggested) in AsyncTask (in any case, you will have such a more beautiful implementation).

thanks

+5
source

Source: https://habr.com/ru/post/1501793/


All Articles