Implementation of JAVA multithread Thread and Runnable

  • 2020-04-01 01:36:15
  • OfStack

Only a single inheritance is allowed in Java, but multiple interfaces are allowed, so the second approach is more flexible.



    public void startOne() {
        //Create an instance
        OneThread oneThread = new OneThread();
        //Start thread ThreadA
        oneThread.startThreadA();
        try {
            //Set the thread to sleep for 1 second
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //Stop the thread, why not use the stop() method here, because the method is deprecated but can be used in deadlocks.
        oneThread.stopThreadA();
    }



    public void startTwo() {
        //Create an instance
        Runnable runnable = new TwoThread();
        //Put the instance into the thread
        Thread threadB = new Thread(runnable);
        //Starting a thread
        threadB.start();
    }


//The class Thread defines threads
class OneThread extends Thread {
    private boolean running = false;
    public void start() {
        this.running = true;
        super.start();
    }
    public void run() {
        int i = 0;
        try {
            while (running) {
                System.out.println(" inheritance Thread Class defines the threaded body of a program ......" + i++);
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void startThreadA() {
        System.out.println(" Start the inheritance Thread Class definition thread ");
        this.start();
    }
    public void stopThreadA() {
        System.out.println(" Close the inheritance Thread Class definition thread ");
        this.running = false;
    }
}


//Implement the Runnable interface to define threads
class TwoThread implements Runnable {
    private Date runDate;
    public void run() {
        System.out.println(" implementation Runnable The interface defines the threaded body of the program ......");
        this.runDate = new Date();
        System.out.println(" Thread startup time ......" + runDate);
    }


public static void main(String[] args) {
        //Instantiate object
        ThreadStartAndStop threadStartAndStop = new ThreadStartAndStop();
        threadStartAndStop.startOne();
        threadStartAndStop.startTwo();
    }

  Starts the inherited Thread class definition Thread
Inheriting the Thread class defines the body of a threaded program... 0
Inheriting the Thread class defines the body of a threaded program... 1
Inheriting the Thread class defines the body of a threaded program... 2
Inheriting the Thread class defines the body of a threaded program... 3
Inheriting the Thread class defines the body of a threaded program... 4
Close the inherited Thread class definition Thread
Implement the Runnable interface to define the threaded body of the program...
Thread startup time... Fri Mar 15 12:56:57 CST 2013


Related articles: