开发者

how to stop infinite loop threads from android service?

开发者 https://www.devze.com 2023-04-09 13:13 出处:网络
i am working with threads in sample application.In my application i have used 3 threads are running in infinite loop.These 3 threads are used in android service class.when i am starting these threads

i am working with threads in sample application.In my application i have used 3 threads are running in infinite loop.These 3 threads are used in android service class.when i am starting these threads then the threads are running and UI is not allowing until completion of infinite loop.but how can i stop the threads and how can i handle UI?

i have written a service class as follows:

ServiceApp.java

    public class ServiceApp extends Service
    {
  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }

@Override
public void onCreate() {
    super.onCreate();

    while(true)
    {
    Thread child1=new Thread(new Runnable() {

        @Override
        public void run() {

            try {

                function1();

            } catch (InterruptedException e) {

                e.printStackTrace();
            }

        }
    });

    child1.start();

    Thread child2=new Thread(new Runnable() {

        @Override
        public void run() {

            try {


                function2();



            } 
            catch (InterruptedException e) {

                e.printStackTrace();
            }
        }
    });

    child2.start();


    Thread child3=new Thread(new Runnable() {

        @Override
        public void run() {

            try {


                function3();


            } catch (InterruptedException e) {

                e.printStackTrace();
            }       
        }
    });

    child3.start();

Toast.makeText(ServiceApp.this, "All threads started", Toast.LENGTH_SHORT).show();

}
}

public void function1() throws InterruptedException
{
    Generic generic=new Generic(); //this for connect to web services

    String add=generic.getAdd(10,20);

    Log.v("function1", "addition from service"+add); 

    Thread.sleep(1000);

}

public void function2() throws InterruptedException
{

    Generic generic=new Generic(); //this for connect to web services

    String sub=generic.getSub(34,20);

    Log.v("function2", "subtraction from service"+sub); 

    Thread.sleep(1000);
}

public void function3() throws InterruptedException
{

    Generic generic=new Generic(); //this for connect to web services

    String mul=generic.getMul(4, 6);

    Log.v("function3", "multipicationn from service"+mul); 

开发者_运维技巧    Thread.sleep(1000);
}

}

how can i stop child1,child2,child3 threads from activity class?

please any body help me?


Service.onCreate() is executed inside the UI thread. You have an infinite loop there, that continuously creates more and more threads, so UI doesn't get a chance to respond to user's actions. If you actually intend to have so many threads, you need to create another thread that would start the original three.

Activity can communicate with the service either via Binder (you'll need to return an actual implementation instead of null there) or by sending intents, which you can capture and process in Service.onStart()

0

精彩评论

暂无评论...
验证码 换一张
取 消