new Runnable() but no new thread?

前端 未结 8 2123
名媛妹妹
名媛妹妹 2020-12-08 11:11

I\'m trying to understand the code here , specifically the anonymous class

private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
   final l         


        
相关标签:
8条回答
  • 2020-12-08 11:23

    A thread is something like some branch. Multi-branched means when there are at least two branches. If the branches are reduced, then the minimum remains one. This one is although like the branches removed, but in general we do not consider it branch.

    Similarly when there are at least two threads we call it multi-threaded program. If the threads are reduced, the minimum remains one. Hello program is a single threaded program, but no one needs to know multi-threading to write or run it.

    In simple words when a program is not said to be having threads, it means that the program is not a multi-threaded program, more over in true sense it is a single threaded program, in which YOU CAN put your code as if it is multi-threaded.

    Below a useless code is given, but it will suffice to do away with your some confusions about Runnable. It will print "Hello World".

    class NamedRunnable implements Runnable {
    
        public void run() { // The run method prints a message to standard output.
            System.out.println("Hello World");
        }
    
        public static void main(String[]arg){ 
            NamedRunnable namedRunnable = new NamedRunnable( );
            namedRunnable.run();
        } 
    }
    
    0 讨论(0)
  • 2020-12-08 11:25

    If you want to create a new Thread...you can do something like this...

    Thread t = new Thread(new Runnable() { public void run() { 
      // your code goes here... 
    }});
    
    0 讨论(0)
  • 2020-12-08 11:32

    Runnable is often used to provide the code that a thread should run, but Runnable itself has nothing to do with threads. It's just an object with a run() method.

    In Android, the Handler class can be used to ask the framework to run some code later on the same thread, rather than on a different one. Runnable is used to provide the code that should run later.

    0 讨论(0)
  • 2020-12-08 11:36

    Best and easy way is just pass argument and create instance and call thread method

    Call thread using create a thread object and send a runnable class object with parameter or without parameter and start method of thread object.

    In my condition I am sending parameter and I will use in run method.

    new Thread(new FCMThreadController("2", null, "3", "1")).start();

    OR

    new Thread(new FCMThreadController()).start();

    public class FCMThreadController implements Runnable {
    private String type;
    private List<UserDeviceModel> devices;
    private String message;
    private String id;
    
    
        public FCMThreadController(String type, List<UserDeviceModel> devices, String message, String id) {
    
            this.type = type;
            this.devices = devices;
            this.message = message;
            this.id = id;
        }
    
    
    
        public FCMThreadController( ) {
    
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
    
        }
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-08 11:41

    Runnable is just an interface, which provides the method run. Threads are implementations and use Runnable to call the method run().

    0 讨论(0)
  • 2020-12-08 11:42

    Shouldn't creating a new Runnable class make a new second thread?

    No. new Runnable does not create second Thread.

    What is the purpose of the Runnable class here apart from being able to pass a Runnable class to postAtTime?

    Runnable is posted to Handler. This task runs in the thread, which is associated with Handler.

    If Handler is associated with UI Thread, Runnable runs in UI Thread.

    If Handler is associated with other HandlerThread, Runnable runs in HandlerThread


    To explicitly associate Handler to your MainThread ( UI Thread), write below code.

    Handler  mHandler = new Handler(Looper.getMainLooper();
    

    If you write is as below, it uses HandlerThread Looper.

    HandlerThread handlerThread = new HandlerThread("HandlerThread");
    handlerThread.start();
    Handler requestHandler = new Handler(handlerThread.getLooper());
    

    0 讨论(0)
提交回复
热议问题