am trying to pause a thread and then resume it but when i do the pause it freeze the application. i try several things but with no luck.
on my main activity am calling the thread and have the button that will pause the thread and a second runnable class it start running and draw on my plot which include and the pause function. the code that i implement is the follow
Main activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDoctorDynamicXYDatasource= new DoctorDynamicXYDatasource(this, mHandler);
findViewById(R.id.Pause).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
synchronized(pp){
mDoctorDynamicXYDatasource.Pause();
Log.i("File", "Pause button ");
}
}
});
findViewById(R.id.Start).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
draw();
}
});
public void draw(){
///some code
pp= new Thread(data);
pp.start();
}
and on the **DoctorDynamicXYDatasource class** am doing the follow
public class DoctorDynamicXYDatasource extends Activity implements Runnable {
public void run() {
//loading and draw on the plot
}
public void Pause() {
synchronized (Thread.currentThread()) {
Log.i("File","pause");
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
how can i pause the thread and then start it again. when i pause the thread it lock instead only the draw plot all the screen and the button without be able to make a selection
EDIT
i add the code that you tell me and from the main activity with the pause button am calling the pause fuinction from my DoctorDynamicXYDatasource class but it's not synchronized in order to wait the thread. am also not allow to call the pause function from other class?
mDoctorDynamicXYDatasource= new DoctorDynamicXYDatasource(this, mHandler);
findViewById(R.id.Pause).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("File", "Pause button pressed");
mDoctorDynamicXYDatasource.pause();
}
});
You are calling pause on the UI thread, which indeed results in freezing the application.
you have to pause your specific thread from the inside, like this:
public void run() {
// Your code here, there is probably a loop
while (someCondition) {
// Loop work
synchronized(this) {
if (pause) {
pause = false;
wait();
}
}
}
}
public synchronized void pause() {
pause = true;
}
public synchronized void go() {
notify();
}
Calling pause will allow the thread to wait at the next iteration. Calling go will allow the thread to exit the wait state immediately.
Afaik, there is no way to force a Thread to pause unless you are in the thread.
Related
I try to make a progressbar on a button click. the progressbar is only visible after everything else is procesed.
MainActivity.this.runOnUiThread(new Runnable(){
#Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
} });
Can I force the UI thread to join or something like that?
EDIT: Complete onClick code
public void onClick(View view) {
if (view == etLocation){
etLocation.setText("");
}
if (view == btnGo){
//progressBar.setVisibility(View.VISIBLE);
MainActivity.this.runOnUiThread(new Runnable(){
#Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
} });
Thread buttonpress = new Thread(new Runnable() {
public void run()
{
buttonGo();
}
});
buttonpress.run();
}
if (view == datePicker){
new DatePickerDialog(MainActivity.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
runOnUiThread is async. Looking through your code, I see an easy fix to it too:
//progressBar.setVisibility(View.VISIBLE);
Thread buttonpress = new Thread(new Runnable() {
public void run()
{
buttonGo();
}
});
//Move runOnUiThread down. The thread above will not start doing its thing until you tell it to run
MainActivity.this.runOnUiThread(new Runnable(){
#Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
buttonpress.run();//TO HERE. Now the progressbar will be initialized, THEN the thread will start
} });
//buttonpress.run(); MOVE THIS
What I basically did was move runOnUiThread down, so it can access the ButtonPress thread.
Then, after the progressBar is visible, you run the thread.
The UI thread is intentionally asynchronous. The job of the application layer is to tell the UI thread what to do, not when to do it.
If your application logic depends on the UI updating, then you should consider your application logic to be too tightly coupled anyway.
I have started the Thread, In that thread i am trying to connect to the server, After receiving the response, I have to update the UI with event listeners(implemented through Interface). Here After receiving the response i need to show the popup Dialog once user clicks OK, need to continue the thread and complete the other process.
class ConnectionThread extends Thread {
ConnectionThread() {
this.setName("ConnectionThread");
}
#Override
public void run() {
// Need to pause the thread for sometime, Need to do the functionality here.
((Activity)mContext).runOnUiThread(new Runnable() {
public void run() {
// custom dialog
showAlertDialog();
// start the thread functionality again from that position.
}
});
}
I have tried with wait() concept and also join, which are not helped as expected. Any help appreciated.
you can use countdownlatch
class ConnectionThread extends Thread {
CountDownLatch countDownLatch = new CountDownLatch(1);
public ConnectionThread() {
this.setName("ConnectionThread");
}
#Override
public void run() {
try {
sleep(2000);
runOnUiThread(new Runnable() {
#Override
public void run() {
//update ui then
countDownLatch.countDown();
}
});
countDownLatch.await();
//start process again
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
i have a button with onclicklistener that download a picture from internet and update progress-bar in UI thread . when users click on the button for first time , it work correctly , but if the users click on the button for seconds &... when download is not completed , a duplicate process happens .how could i get rid of this problem?
Button btnDownload = (Button) findViewById(R.id.btndownload);
final TextView txtcaption = (TextView) findViewById(R.id.txtcaption);
final ProgressBar progress = (ProgressBar) findViewById(R.id.progress);
btnDownload.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
new Thread(new Runnable() {
OnProgressDownloadListener listener = new OnProgressDownloadListener() {
#Override
public void progressDownload(final int percent) {
new HANDLER.post(new Runnable() {
#Override
public void run() {
progress.setProgress(percent);
txtcaption.setText(percent + " %");
if (percent >= 100) {
txtcaption.setText("completed");
Toast.makeText(activity.this, "download completed", Toast.LENGTH_SHORT).show();
}
}
});
}
};
#Override
public void run() {
//my download manager
FileDownloader.download("address/file", DIR + "/file");
}
}).start();
}
});
}
An easy way to do this would be the following...
First, begin by declaring a thread...
Thread myThread
Then create a simple method that contains the thread you wish to execute when the button is pressed...
private void getPicture()
{
myThread = new Thread()
{
public void run()
{
// Place thread code here...
}
};
myThread.start();
}
Then you can do a simple check when the button is pressed and, if the thread is active, don't call the getPicture method...buttonDownload.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
if (myThread.isAlive())
{
// Thread is alive, do not launch again
}
else
{
// Thread is not running so call method...
getPicture();
}
}
});
Have a Thread variable in your class that's initialized to NULL. In your onClickListener, check the value of that variable. If its null, start a new thread and save the value of that thread in the variable. If it isn't, ignore the button press or pop up a downloading toast. Remember to set the variable back to null when your thread is completed.
I'd highly recommend using an AsyncTask for this rather than a thread, it will be cleaner.
Can someone explain to me 2 things about the thread code that I finally made almost working the way it should. I want to do a periodic task in the background every x seconds and be able to stop and start it at will. I coded that based on the examples I found, but I'm not sure if I made it in the right way. For the purpose of debugging, the task is displaying a time with custom showTime().
public class LoopExampleActivity extends Activity {
TextView Napis, Napis2;
Button button1,button_start,button_stop;
Handler handler = new Handler();
Boolean tread1_running = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Napis = (TextView) findViewById(R.id.textView1);
Napis2 = (TextView) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button_stop = (Button) findViewById(R.id.button_stop);
button_start = (Button) findViewById(R.id.button_start);
button_stop.setOnClickListener(new OnClickListener() {
#Override
public void onClick (View v) {
tread1_running = false;
}
});
button_start.setOnClickListener(new OnClickListener() {
#Override
public void onClick (View v) {
tread1_running = true;
}
});
thread.start();
}// endof onCreate
final Runnable r = new Runnable()
{
public void run()
{
handler.postDelayed(this, 1000);
showTime(Napis2);
}
};
Thread thread = new Thread()
{
#Override
public void run() {
try {
while(tread1_running) {
sleep(1000);
handler.post(r);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
Now the questions are:
1)Will my thread quit forever if I stop it with the stop button?
2)Why can't I start it again with the start_button? If I add the tread.start() in a button, will it crash?
3) I tried a second version when I let the thread run and put a condition into the handler. The only way I can get it to work is to loop conditionaly in the handler by adding an
if (thread1_running) {
handler.postDelayed(this, 2000);
showTime(Napis2);
}
And changing the condition in a thread start to while (true) but then I have an open thread that is running all the time and I start and stop it in a handler, and it posts more and more handlers.
So, finally I get to the point it looks like that:
final Runnable r = new Runnable()
{
public void run()
{
if (thread1_running) handler.postDelayed(this, 1000);
showTime(Napis2);
}
};
Thread thread = new Thread()
{
#Override
public void run() {
try {
while(true) {
sleep(1000);
if (thread1_running) handler.post(r);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Is the proper way to do that is to start and stop a whole thread? Or that is the best way?
The best way to achieve something like that would be, in my humble opinion, to postDelayed(Runnable, long).
You could do something like this. Class definition:
private Handler mMessageHandler = new Handler();
private Runnable mUpdaterRunnable = new Runnable() {
public void run() {
doStuff();
showTime(Napis2);
mMessageHandler.postDelayed(mUpdaterRunnable, 1000);
}
};
And control true run/stop like this:
To start:
mMessageHandler.postDelayed(mUpdaterRunnable, 1000);
And to stop:
mMessageHandler.removeCallbacks(mUpdaterRunnable);
It's much much simpler, in my humble opinion.
Threads a described by a state machine in java.
When a thread get outs of its run method, it enters in the stopped state and can't be restarted.
You should always stop a thread by getting it out of its run method as you do, it s the proper way to do it.
If you want to "restart the thread", start a new instance of your thread class.
You should better encapsulate your thread and its running field. It should be inside your thread class and the class should offer a public method to swich the boolean. No one cares about your datastructure, hide them. :)
You should consider using runOnUIThread for your runnable, its much easier to use than handlers.
I wrote this app that in the first screen it has an included Thread on it. So it has I timed it like 7 seconds then it will proceed to the next activity.
The problem is whenever I hit the home button the music will stop and it will go to android homescreen but after my timed is done which is the 7 seconds, the app will reappear and will show the next activity.
I tried putting finish(); in the onpause(); but it's still showing the next activity.
here's the actual code.
public class HelloWorldActivity extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mp = MediaPlayer.create(this, R.raw.otj);
mp.start();
Thread LogoTimer = new Thread(){
public void run(){
try{
int LogoTimer = 0;
while(LogoTimer < 7000){
sleep(100);
LogoTimer = LogoTimer + 100;
}
startActivity(new Intent("com.example.HelloWorld.CLEARSCREEN"));
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
finish();
}
}
};
LogoTimer.start();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
First, that's a really inefficient way to run a timer. Try this way instead:
new Handler().postDelayed(new Runnable() {
public void run() {
// Do some work.
}
}, delayTimeInMs);
Second, your starting a new activity when that timer eventually fires. It doesn't matter that the originating activity is finished. Your startActivity() is running on it's own thread and will execute regardless.
It's possible the postDelayed() method will function like you expect. If not you'll need to have it check when it runs whether it should really start the activity. However, I think the Handler is attached to the default Looper which means it will stop (or rather, the message won't be posted) if the main activity finishes.
The application is still in the background and the thread is not destroyed so it will fire the startActivity.
I would not really setup a splash screen this way, or use a thread unless I wanted it off the UI for some reason, even then there are better options.
For educational purposes to take care of this you need to be able to abort the thread safely in onPause() one way to do so is below
Modifed Thread
Thread LogoTimer = new Thread() {
private volatile boolean abortThread = false;
public void run(){
long stopAt = System.currentTimeMillis() + 7000;
while (!abortThread && stopAt > System.currentTimeMillis())
yield();
if (!abortThread)
startActivity ...
}
public synchronized void stopThread() {
abortThread = true;
}
};