How many ways are for creating a new thread in Java?

后端 未结 7 2140
清歌不尽
清歌不尽 2020-12-29 10:14

Actually, what other ways are available apart from extending the Thread class and implementing the Runnable interface?

相关标签:
7条回答
  • 2020-12-29 10:34

    Threads can be created mainly in 3 different ways

    1. Extend the java.lang.Thread class'
    
    class SampleThread extends Thread {
    
        //method where the thread execution will start 
        public void run(){
            //logic to execute in a thread    
        }
    
        //let’s see how to start the threads
        public static void main(String[] args){
           Thread t1 = new SampleThread();
           Thread t2 = new SampleThread();
           t1.start();  //start the first thread. This calls the run() method.
           t2.start(); //this starts the 2nd thread. This calls the run() method.  
        }
    } 
    
    1. Implement the java.lang.Runnable interface
    
    class A implements Runnable{
    
        @Override
        public void run() {
    
            // implement run method here 
        }
    
        public static void main() {
            final A obj = new A();
    
            Thread t1 = new Thread(new A());
    
            t1.start();
        }
    
    
    }
    
    1. Implement the java.util.concurrent.Callable interface
    
    class Counter implements Callable {
    
        private static final int THREAD_POOL_SIZE = 2;
    
        // method where the thread execution takes place
        public String call() {
            return Thread.currentThread().getName() + " executing ...";
        }
    
        public static void main(String[] args) throws InterruptedException,
                ExecutionException {
            // create a pool of 2 threads
            ExecutorService executor = Executors
                    .newFixedThreadPool(THREAD_POOL_SIZE);
    
            Future future1 = executor.submit(new Counter());
            Future future2 = executor.submit(new Counter());
    
            System.out.println(Thread.currentThread().getName() + " executing ...");
    
            //asynchronously get from the worker threads
            System.out.println(future1.get());
            System.out.println(future2.get());
    
        }
    }
    

    Favor Callable interface with the Executor framework for thread pooling.

    The Runnable or Callable interface is preferred over extending the Thread class

    0 讨论(0)
  • 2020-12-29 10:39

    There is exactly one way to create a new thread in Java and that is to instantiate java.lang.Thread (to actually run that thread you also need to call start()).

    Everything else that creates threads in Java code falls back to this one way behind the cover (e.g. a ThreadFactory implementation will instantiate Thread objects at some point, ...).

    There are two different ways to specify which code to run in that Thread:

    • Implement the interface java.lang.Runnable and pass an instance of the class implementing it to the Thread constructor.
    • Extend Thread itself and override its run() method.

    The first approach (implementing Runnable) is usually considered the more correct approach because you don't usually create a new "kind" of Thread, but simply want to run some code (i.e. a Runnable) in a dedicated thread.

    0 讨论(0)
  • 2020-12-29 10:40

    for creating a thread there is only one way in java

    ie. Thread class start() method but there are different ways to run the thread by using different ways

    like 1.Thread 2.Runnable 3.RunnableFeature 4.Callable
    5.ERxecutorService...etc

    0 讨论(0)
  • 2020-12-29 10:41

    There are actually total 4 ways to create thread in java :

    1. By extending java.lang.Thread class
    2. By implementing java.lang.Runnable interface
    3. By using anonymous inner class
    4. By implementing Callable interface.
    0 讨论(0)
  • 2020-12-29 10:43

    Or you can create a Callable which is an interface which is similar to Runnable except that it defines a method call that can return a value. To instantiante a Callable, you can pass it to an executor. You can find a full explanation of multithreading and callable examples here

    0 讨论(0)
  • 2020-12-29 10:43

    The preferred way of starting threads in Java 6 is using Executors:

        ExecutorService es = Executors.newCachedThreadPool();
        Runnable r = <your runnable here>;
        es.execute(r);
    
    0 讨论(0)
提交回复
热议问题