Java multithreading learning Summary-thread overview and thread creation methods (1)

Source: Internet
Author: User

In Java Development, multithreading is very common. If it is used well, the program performance can be improved. First, let's take a look at the differences between threads and processes: 1. An application is a process, and one or more threads exist in a process. A process must have at least one main thread. A thread can be viewed as a lightweight process. (Lightweight process) 2. Multiple Threads can share the resources of a process. Processes are independent. A process cannot share resources of other processes. 3. Because the system creates a process and needs to allocate space for it, the cost of creating a process is high, and the cost of creating a thread is much lower. Thread creation method: Java creates multithreading in three ways: 1, inherits the Thread class. After a class inherits the Thread class and overwrites the run method, if you create an instance of the class and call the start method, the system starts a new Thread and runs the run method. The Code is as follows: Copy code 1 public class ThreadApp {2 public static void main (String [] args) {3 // create thread 4 MyThread thread = new MyThread (); 5 // start thread 6. start (); 7} 8} 9 10 11 class MyThread extends Thread {12 13 private int I = 0; 14 15 public void run () {16 for (; I <5; I ++) {17 System. out. println (getName () + ":" + I); 18} 19} 20} copy Code 2 to implement the Runnable interface. Define a class to implement the Runnable interface, create an instance of the class, create a Thread object, use the Runnable instance as the target of the Thread object, and finally call the start method of the Thread object. During execution, the Thread object calls the run method of the Runnable object. The Code is as follows: copy the code public class ThreadApp {public static void main (String [] args) {// create the runnable object MyRunnable target = new MyRunnable (); // create the thread object, use runnable as the target thread Thread = new thread (target); // call the start method of the thread. During thread execution, the runnable method of the target Thread is called. start () ;}} class MyRunnable implements Runnable {private int I = 0; @ Override public void run () {while (I <500) {synchronized (this) {lead E M. out. println (Thread. currentThread (). getName () + ":" + I ++) ;}}} copy code 3 to implement the Callable interface. Callable is a generic interface. This method of creating multiple threads can obtain the return value after the thread is executed. The creation procedure is: Define a class MyCallable to implement the Callable interface, create an instance of MyCallable, and then create the FutureTask object target to wrap the Callable object, because the FutureTask class implements the Runnable interface, so it can be used as the target attribute of the Thread. Finally, create a Thread. After the thread completes execution, call the get method of FutureTask to obtain the returned value. The Code is as follows: copy the code public class ThreadApp {public static void main (String [] args) {// create Callable object MyCallable callable = new MyCallable (); // create a FutureTask object to wrap callable. The FutureTask class implements the Runnable interface, so it can be used as the target FutureTask of the Thread class <String> target = new FutureTask <> (callable ); // create Thread thread = new Thread (target); // start the thread Thread. start (); try {// obtain the thread execution result String result = target. get (); System. out. println (resu Lt);} catch (InterruptedException | ExecutionException e) {e. printStackTrace () ;}} class MyCallable implements Callable <String >{@ Override public String call () throws Exception {synchronized (this) {System. out. println (Thread. currentThread (). getName (); Thread. sleep (1000); return new Date (). toString () ;}}: it is easiest to use a Thread to create a Thread. The getName () method can be used to directly obtain the name of the current Thread. But not flexible enough. Multiple Threads cannot share the attributes of a Thread. Runnable and Callable are the same. Both interfaces are implemented first, and then the instance of the implementation class is used as the target of the Thread to create a Thread. You can use the Callable interface to create a thread with a returned value. In addition, multiple threads can share the target attributes. The Code is as follows: copy the code public class ThreadApp {public static void main (String [] args) {MyRunnable target = new MyRunnable (); // two threads use the same target, the attributes Thread thread1 = new Thread (target); Thread thread2 = new Thread (target); thread1.start (); thread2.start () ;}} in target can be shared ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.