Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java.lang.UnsupportedOperationException when Thread stop?

My codes below:

 public class SimpleService extends Service {
    private MyThread myythread;
    public boolean isRunning = false;
    long interval=30000;
    @Override
    public IBinder onBind(Intent arg0) {
    return null;
    }
    @Override
    public void onCreate() {
    super.onCreate();
    myythread  = new MyThread(interval);
    }
    @Override
    public synchronized void onDestroy() {
    super.onDestroy();
    if(!isRunning){
    myythread.interrupt();
    myythread.stop();
    }
    }
    @Override
    public synchronized void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    if(!isRunning){
    myythread.start();
    isRunning = true;
    }
    }
    class MyThread extends Thread{
    long interval;
    public MyThread(long interval){
    this.interval=interval;
    }
    @Override
    public void run(){
    while(isRunning){
    System.out.println("Service running");
    try {
    fileUpload();
    Thread.sleep(interval);
    } catch (InterruptedException e) {
    isRunning = false;
    e.printStackTrace();
    }
    }
    }
    private void fileUpload() {
    String url=Environment.getExternalStorageDirectory()+"/new_Account.txt";
    if(checkInternetConnection()){
    boolean upload_status=new UploadToFtp().ftpUpload1(url, "new_Account.txt");
    }
    }
    }
    private boolean checkInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
    return true;
    }
    else
    {
    return false;
    }
    }
    }

When call

@Override
    public synchronized void onDestroy() {
    super.onDestroy();
    if(!isRunning){
    myythread.interrupt();
    myythread.stop();
    }
    }

then throw exception :

java.lang.UnsupportedOperationException

How to solve this exception. please help me anybody.

like image 209
kablu Avatar asked Mar 07 '26 21:03

kablu


2 Answers

As of JDK8, Thread.stop is really gone. It is the first deprecated method to have actually been de-implemented. It now just throws UnsupportedOperationException

http://cs.oswego.edu/pipermail/concurrency-interest/2013-December/012028.html http://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

like image 106
Bobz Avatar answered Mar 10 '26 13:03

Bobz


You can follow the below link

Please refer my solution at: Why java.lang.UnsupportedOperationException when Thread stop?

The solution of using a TimerTask instead of the thread can server your purponse better and more optimized.

Even the cancellation would work :)

P.S: It also has information why stop is not working for you.

[Reference :][Unsupported operation exception in thread]2

like image 43
nurealam11 Avatar answered Mar 10 '26 14:03

nurealam11