I have two processes "Loaddata(); Workoffline();", now I want the process Loaddata(); to run until it is completed, the application can switch to the next process Workoffline(); .But I do not know how to do, expect people to help me.
public void test(){
runOnUiThread(new Runnable() {
#Override
public synchronized void run() {
loaddata();
//your UI interaction code here
}
});
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
workoffline();
}
});
}
- The best approach will be to use CountDownLatch in java.util.concurrent package.
- You can use CountDownLatch's method countDown() to notify the completion of the loaddata(), and then use await() method to allow the execution of workoffline().
- Another way is to use join()
Eg:
Thread t1 = new Thread(new Runnable(){
public void run(){
handler.post(new Runnable(){ // Declare the Handler handler = new Handler();
// in onCreate()
public void run(){
loaddata();
}
});
}
});
t1.start();
try{
t1.join();
}catch(InterruptedException ex){
}
Thread t2 = new Thread(new Runnable(){
public void run(){
handler.post(new Runnable(){
public void run(){
workoffline();
}
});
}
});
t2.start();
Related
I use this new thread from a method called from onCreate().
info is a textView().
new Thread(new Runnable() {
public void run() {
info.post(new Runnable(){
public void run() {
info.setText(panel.getInfo());
}
});
}
}).start();
If info.setText(panel.getInfo()); call without creating the thread take 3-4 seconds being blocked application but showing it,
then how can I show the text without being blocked the app¿?
new Thread(new Runnable() {
public void run() {
String text = panel.getInfo();
info.post(new Runnable(){
public void run() {
info.setText(text);
}
});
}
}).start();
Actually what is taking long is your panel.getInfo() call. And your a making this call in the info.post, so you're doing it in the UI thread.
First of all, I think you should move code String text = panel.getInfo(); from info.post method, because it will be executed at UI thread. Do something like this:
new Thread(new Runnable() {
public void run() {
String text = panel.getInfo();
info.post(new Runnable(){
public void run() {
info.setText(text);
}
});
}
}).start();
Secondly, I think you should save your thread in class member, because GC may destroy your Thread, before it be in time to do something. So, do something like this:
mThread = new Thread(new Runnable() {
public void run() {
String text = panel.getInfo();
info.post(new Runnable(){
public void run() {
info.setText(text);
}
});
}
}).start();
Where mThread is a class member:
public abstract class MyActivity extends ActionBarActivity{
private Thread mThread;
/*Other code*/
}
i created running app and i trying to create stopper that starting when user press start button.
but when i using thread the application return black screen
,this is my onCreate method i added thread
Thread t = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
SetTextOnView();
}
});
}
};
t.start();
the SetTextOnView method contain
public void SetTextOnView()
{
TextView timedisp = (TextView) findViewById(R.id.stopperdisplay);
while(true)
{
timedisp.setText(String.valueOf(total));
}
}
the problem start when i add the while statment
why is that?
Do the loop in your thread to not block the ui thread + add some delay to not block the ui thread :
Thread t = new Thread() {
public void run() {
while (true) {
runOnUiThread(new Runnable() {
#Override
public void run() {
SetTextOnView();
}
});
Thread.sleep(1000);
}
}
};
And your SetTextOnView :
public void SetTextOnView()
{
TextView timedisp = (TextView) findViewById(R.id.stopperdisplay);
timedisp.setText(String.valueOf(total));
}
I want to implement ProgressBar in Android and when I execute the program, Progressbar should show for up to 2 seconds. I can't get it to work properly but I can't figure out why.
public void myThread(){
Thread th=new Thread(){
#Override
public void run(){
try
{
while(mRunning)
{
Thread.sleep(10L);//10s wait
YourCurrentActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
//DISMISS PROGRESS BAR HERE
mRunning=false;
}
});
}
}catch (InterruptedException e) {
// TODO: handle exception
}
}
};
th.start();
}
I have tried this but it does not giving me output as i want.
That what handlers are for in Android.
Example:
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// cancel or dismiss your progressbar
}
}, 2000);
I want to turn on/off the flash light in infinite loop, so when it turned on it should wait for 5 seconds and then turned off then wait 5 seconds to turned on again, and so on...
how I can do that?
here is my code:
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// num = Integer.parseInt(n.getText().toString());
while(bl){
if(camera == null){
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
turnOn();
}
});
}
}).start();
}
else{
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
turnOff();
}
});
}
}).start();
}
}
}
});
I would recommend not using Threads in order to achieve this. Why not use the Runnable class and post it with a delay via a Handler? For example:
Handler handler = new Handler(); // make this a member variable of your class
boolean isOn = false; // make this a member variable of your class
final Runnable flashRunnable = new Runnable() {
#Override
public void run() {
if (isOn) {
turnOff();
isOn = false;
} else {
turnOn();
isOn = true;
}
handler.postDelayed(flashRunnable, 5000);
}
};
handler.postDelayed(flashRunnable, 5000);
If you need to run the code inside the Runnable on the UI thread, you even call postDelayed on a View instead of creating a Handler
Try something like so, using Executors instead of (ugly) Thread.sleep():
boolean cameraOn = true
final Runnable runnable = new Runnable() {
#Override
public void run() {
// your logic here:
// if (cameraOn) ...
// else ...
// cameraOn = !cameraOn
}
};
Executors.newScheduledThreadPool(1).schedule(new Runnable() {
#Override
public void run() {
runnable.run();
}
}, 5, TimeUnit.SECONDS);
How to make a delay that could call a function after some time but the thread still should be running. Is there any better way than this.
new Thread(new Runnable() {
public void run(){
try{ Thread.sleep(recordtime); }
catch(Exception e){}
runOnUiThread(new Runnable() {
#Override
public void run() {
reset();
}
});
}
}).start();
To run some code on Ui thread , after some delay:
Handler h = new Handler(Looper.getMainLooper());
Runnable r = new Runnable() {
#Override
public void run() {
//--code run in Main, UI thread
}
};
h.postDelayed(r,2000); //-- run after 2 seconds
Handler require a Looper on target thread. UI thread already has it, other threads need to be configured first.
Other options are:
Timer:
Timer t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
//--code run in separate thread
}
},2000);
And ScheduledExecutorService:
ScheduledExecutorService se = Executors.newSingleThreadScheduledExecutor();
se.schedule(new Runnable() {
#Override
public void run() {
//--code run in separate thread
}
},2, TimeUnit.SECONDS);