Instance analysis Java single-thread and multi-thread, instance analysis java single-Thread
Thread: each task is called a thread. A thread cannot exist independently and must be part of a process.
Single thread: Generally, common Java applications are single-threaded. For example, when a helloworld program is run, the jvm process is started and the main method is run to generate a thread, the main method is also called the main thread.
Multithreading: A program running more than one thread is called a multi-threaded program. multithreading can help programmers write efficient programs to make full use of CPU.
Example of Single-threaded code:
Public class SingleThread {public static void main (String [] args) {Thread thread = Thread. currentThread (); // get the currently running thread object thread. setName ("single thread"); // rename the System thread. out. println (thread. getName () + "running"); for (int I = 0; I <10; I ++) {System. out. println ("the thread is sleeping:" + I); try {thread. sleep (1000); // thread sleep, delayed by one second} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace (); System. out. println ("thread error ");}}}}
Example of multi-threaded code:
Note: multithreading can be implemented in two ways: Inheriting the Thread class and implementing the Runnable interface.
Implement multithreading by inheriting the Thread class
Public class TestThread {public static void main (String [] args) {Thread t1 = new ExtendThread ("t1", 1000); // create a Thread using a conversion object, thread t2 = new ExtendThread ("t2", 2000); Thread t3 = new ExtendThread ("t3", 3000); t1.start (); // start the Thread and call the run method t2.start (); t3.start () ;}} class ExtendThread extends Thread {// inherit the Thread class String name; int time; public ExtendThread (String name, int time) {// construct the thread name and sleep time this. name = name; this. time = time;} public void run () {// rewrite the run method of the Thread class try {sleep (time); // All threads add sleep} catch (InterruptedExceptione) {e. printStackTrace (); System. out. println ("thread interruption exception");} System. out. println ("name:" + name + ", thread sleep:" + time + "millisecond ");}}
Implement multiple threads of the Runnable interface
Public class RunnableThread {public static void main (String [] args) {Runnable r1 = new ImplRunnable ("r1", 1000 ); // The Runnable interface must rely on the Thread class to create Thread t1 = new Thread (r1); // Runnable cannot call the start () method because it is not a Thread, so we need to add the Thread Runnable r2 = new ImplRunnable ("r2", 2000); Thread t2 = new Thread (r2); Runnable r3 = new ImplRunnable ("r3 ", 3000); Thread t3 = new Thread (r3); t1.start (); // start the Thread and call the run method t2.start (); t3.start ();}} class ImplRunnable implements Runnable {// inherits the class String name of the Runnable interface; int time; public ImplRunnable (String name, int time) {// constructs the thread name and sleep time this. name = name; this. time = time ;}@ Overridepublic void run () {// run method to implement Runnable try {Thread. sleep (time); // All threads add sleep} catch (InterruptedException e) {e. printStackTrace (); System. out. println ("thread interruption exception");} System. out. println ("name:" + name + ", thread sleep:" + time + "millisecond ");}}
Note: The Thread class actually implements the Runnable interface.
The Runnable interface has advantages over inheriting the Thread class.