mercredi 4 mars 2015

How to make an Android thread start only when another Android thread ends



I want this android program to have a 10 second countdown before a File is written and then read and set as the text on the MainActivity. I want the mCounterThread to finish its task before mFileThread starts. Since onPause() and onResume() may elongate the amount of required time I can't do a .sleep(milliseconds) with a fixed time. I tried thread.join() but this does not seem to work. Why? Could I use another notify/wait? Thank you



public class MainActivity extends Activity {
private TextView mText;
private EditText mUserInput;
private CounterThread mCounterThread;
private MakingFileThread mFileThread;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = (TextView)findViewById(R.id.text);
mUserInput = (EditText)findViewById(R.id.userInput);
mCounterThread = new CounterThread();
}

@Override
public synchronized void onPause(){
super.onPause();
mCounterThread.onPause();
}

@Override
public synchronized void onResume(){
super.onResume();
mCounterThread.onResume();
}

public void startCounterThread(){
mCounterThread.start();
}

public void startMakingFileThread(String s1,String s2){
mFileThread = new MakingFileThread(s1,s2);
mFileThread.start();
}

public void button_handler(View v){
String val = mUserInput.getText().toString();
startCounterThread();
String file = "myfile.txt";
startMakingFileThread(val, file);//I want this to start only
//once mCounterThread has finished its countdown.
}

public void updateSeconds(final long seconds){
Runnable UIdoWork = new Runnable(){
public void run(){
String time = String.valueOf(seconds);
mText.setText("Your file will open in " + time + " seconds");
}
};
runOnUiThread(UIdoWork);
}


public void updateWriteFile(final String userText){
Runnable UIdoWork = new Runnable(){
public void run(){
mText.setText(userText);
}
};
runOnUiThread(UIdoWork);
}

private class CounterThread extends Thread{
private int count = 10;
private final Object lock = new Object();
private volatile boolean isRunning = true;

public void onResume(){
if(!isRunning){
isRunning = true;
}
synchronized (lock){
lock.notify();
}
}

public void onPause(){
isRunning = false;
}


@Override
public synchronized void run(){
while(count != 0){
synchronized (lock){
if(!isRunning)try{
lock.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}

try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
updateSeconds(count--);
}

}
}

private class MakingFileThread extends Thread{
private String userInput;
private String fileName;

public MakingFileThread(String s1,String s2){
userInput = s1;
fileName = s2;
}

@Override
public void run(){
try{
sleep(20000);//right now I have this thread
//wait 20 seconds before it starts
}
catch(InterruptedException e) {
e.printStackTrace();
}

try {
FileOutputStream outputStream = openFileOutput(fileName,MODE_PRIVATE);
BufferedWriter writer = new BufferedWriter((new OutputStreamWriter(outputStream,"UTF-8")));
writer.write(userInput);
writer.close();
}catch(Exception e){
e.printStackTrace();
}
try {
FileInputStream fis = openFileInput( fileName );
BufferedReader reader = new BufferedReader((new InputStreamReader(fis,"UTF-8")));
String ans = reader.readLine();
updateWriteFile(ans);
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}


}




Aucun commentaire:

Enregistrer un commentaire