thread-sleep

Java Wake Sleeping Thread

╄→尐↘猪︶ㄣ 提交于 2019-12-05 09:02:42
I did some reading of other post but didn't find an exact answer to what I'm looking for, so I hope someone can give some some clarification. I have a program that will run for some time. I have some threads that run in the back ground that perform various tasks, to keep things simple let think of 3 threads. ThreadA performs a task every 10 seconds, where ThreadB does something every 30 seconds and ThreadC does something every 5 mintues. I don't use busy waiting, and put the threads to sleep for the designated times. My question is regarding a clean shut down. I have a variable that each of

Thread and battery consumption

人盡茶涼 提交于 2019-12-05 05:28:54
I am working on app that check some of the status of phone every 5 seconds. I done it that: Thread checking = new Thread() { public void run(){ while( <<some status>> ) { <<checkstatus and do something>> Thread.sleep(5000); } } } This is bad for the battery? How can I do it in the other way? Service stop working after couple seconds. To answer your question, it depends what you are doing inside the while loop. If you are not using and waking up any "expensive" components (such as internet, bluetooth, other sensors, and screen), you should be fine. Essentially this is how the OS works in the

Android: why i get these AsyncTask Error?

♀尐吖头ヾ 提交于 2019-12-05 01:05:40
问题 i have an asyncTaskProc that read some info from a DB and write it on the ui... the code works perfectcly on Android 4.0 but doesn't work on 2.3... here is the code NEW ASYNCTASK `public class IceCastPoll extends TimerTask { public IceCastPoll() { } @TargetApi(9) public void run() { new AsyncTaskProc().execute(); } }` THE ASYNCTASK implementation @TargetApi(9) class AsyncTaskProc extends AsyncTask<Void, String, Void> { List<Stream> streams=null; @Override protected void onPostExecute(Void

Alternatives to using Thread.Sleep for waiting

拟墨画扇 提交于 2019-12-04 16:03:52
问题 Firstly I am not asking the same question as C# - Alternative to Thread.Sleep?, or Alternative to Thread.Sleep in C#?. I don't think I am using it incorrectly and need a genuine alternative for specific situations. During a code analysis run I saw a surprising violation coming up: Usage of Thread.Sleep() is a sign of flawed design. This violation leads to Peter Richie's article on why exactly this constitutes bad design. We all know thread creation is expensive and blocking in threads means

Long Running Blocking Methods. Difference between Blocking, Sleeping, Begin/End and Async

寵の児 提交于 2019-12-04 14:33:57
This question is not about designs or patterns and which to use. The heart of this question is about what is happening regarding threads and blocking. This example is to apply to any blocking method that is designed to perform the same action continuously. In this case it is a blocking read or write on a networkstream. Is there any appreciable difference behind the scenes as to threading and performance between the methods? My assumption is that each of the methods below creates a thread or uses a pooled thread. Then blocks that thread until there is data to be read. Having said that and in

Is it Really Busy Waiting If I Thread.Sleep()?

一曲冷凌霜 提交于 2019-12-04 01:54:22
My question is a bit nit-picky on definitions: Can the code below be described as "busy waiting"? Despite the fact that it uses Thread.Sleep() to allow for context switching? while (true) { if (work_is_ready){ doWork(); } Thread.Sleep(A_FEW_MILLISECONDS); } PS - The current definition for busy waiting in Wikipedia suggests that it is a "less wasteful" form of busy waiting. Any polling loop, regardless of the time between polling operations, is a busy wait. Granted, sleeping a few milliseconds is a lot less "busy" than no sleep at all, but it still involves processing: thread context switches

Android: why i get these AsyncTask Error?

只愿长相守 提交于 2019-12-03 15:56:56
i have an asyncTaskProc that read some info from a DB and write it on the ui... the code works perfectcly on Android 4.0 but doesn't work on 2.3... here is the code NEW ASYNCTASK `public class IceCastPoll extends TimerTask { public IceCastPoll() { } @TargetApi(9) public void run() { new AsyncTaskProc().execute(); } }` THE ASYNCTASK implementation @TargetApi(9) class AsyncTaskProc extends AsyncTask<Void, String, Void> { List<Stream> streams=null; @Override protected void onPostExecute(Void result) { textSong =(TextView) findViewById(R.id.textViewCurrentSong); textArtist =(TextView) findViewById

Similarity between sleep and join in java

ⅰ亾dé卋堺 提交于 2019-12-02 23:31:41
问题 As the question suggests , I want to know the similarity between the sleep and join methods on the thread. I have gone through many questions that describe the difference between the sleep and join method. But I would like to know different scenarios where the sleep and join methods could be used interchangeably . According to my thinking following code should work just in the same way. I have a main thread in which I start a new thread (just 1) and I want the main thread to wait for the new

How can I draw an image part by part?

*爱你&永不变心* 提交于 2019-12-02 15:08:52
问题 class DrawIma extends JPanel{ protected void paintComponent(Graphics g) { super.paintComponent(g); for (int i=0;i<20;i++){ for (int j=0;j<20;j++) { g.drawImage(BuArr[i*20+j], 20*i, 20*j, 20, 20, null); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } } In this part, BuArr are the 400 blocks divided from a BufferedImage , now i want them to be draw one by one, but the method can not draw the blocks separately, how can i do this? 回答1: Swing is single

Swing JForm freezes until an action is finished [duplicate]

天大地大妈咪最大 提交于 2019-12-02 10:32:33
This question already has an answer here: swing window getting frozen ,not displaying content 1 answer I want to create a Java application that visualizes merge sort using swing components. So far I've written this: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; public class MergeSort extends JFrame { private int[] helper; private int[] heights; private JPanel mainPanel; private JTextArea[] areas; private