Multithreading Summary One: Basic concepts

Source: Internet
Author: User

1. Threads and processes
A, concurrency (concurrency): Only one instruction can be executed at the same time, when multiple process instructions are executed quickly, with the effect of multiple processes executing simultaneously on the macro.
b, parallelism (parallel): At the same time, multiple instructions are executed simultaneously on multiple processors.
C. The operating system can perform multiple tasks at the same time, each of which is a process, and a process can perform multiple tasks at the same time, each task being a thread.
D, the operating system support multi-process concurrency adopted policies are: shared multi-tasking operation policy (Windows3.1, Mac OS9), preemptive multi-task operation policy (Windows NT, Windows 2000 and Unix/linux, etc.).

2. Advantages of Multithreading
A, the process can not share memory, but the sharing of memory between threads is easy;
b, the system needs to reallocate system resources for the process when it is created, but it is much less expensive to create threads, so multi-task concurrency is more efficient than multiple processes using multithreading;
C, the Java language built-in multi-threaded function support, rather than simply as the underlying operating system scheduling, thus simplifying the Java multithreaded programming;

3, multi-threaded application scenarios

A browser must be able to download multiple images at the same time, a Web server must be able to respond to multiple user requests at the same time, and the Java Virtual machine itself provides a super thread in the background for garbage collection, graphics
User interface (GUI) applications also need to initiate separate threads to collect user interface events from the host environment ...

4. Thread creation and startup
A, inheriting the thread class to create a threading class

1 /**2 * @Title: Firstthread.java3 * @Package4  * @authorstudy5 * @date 2014-12-5 pm 3:05:496  * @versionV1.07  */8 9 /**Ten * @ClassName: Firstthread One * @Description: Creating thread classes by inheriting thread A  * @authorstudy - * @date 2014-12-5 pm 3:05:49 -  */ the  Public classFirstthreadextendsThread { -     Private inti; -  -      Public voidrun () { +          for(; i < i++;) { -             //This gets the current thread +             //GetName () gets the current thread name ASystem.out.println ( This. GetName () + "" +i); at         } -     } -  -      Public Static voidMain (string[] args) { -          for(inti = 0; I < 100; i++) { -System.out.println (Thread.CurrentThread (). GetName () + "" +i); in             if(20==i) { -Thread T1 =NewFirstthread (); toT1.setname ("Thread 1"); +Thread t2 =NewFirstthread (); -T2.setname ("Thread 2"); the                  * T1.start (); $ T2.start ();Panax Notoginseng             } -         } the     } +}
Firstthread

b, when you create a thread class by using a method that inherits the thread class, you cannot share instance variables of the thread class between multiple threads.

C. Implement runnable interface Create thread class

1 /**2 * @Title: Secondthread.java3 * @Package4  * @authorstudy5 * @date 2014-12-5 pm 6:53:076  * @versionV1.07  */8 9 /**Ten * @ClassName: Secondthread One * @Description: A  * @authorstudy - * @date 2014-12-5 pm 6:53:07 -  */ the  Public classSecondthreadImplementsRunnable { -     Private inti; -  -     /* + * (Non-javadoc) -      *  + * @see Java.lang.runnable#run () A      */ at @Override -      Public voidrun () { -          for(; i<100;i++){ -System.out.println (Thread.CurrentThread (). GetName () + "" +i); -         } -  in     } -  to      Public Static voidMain (string[] args) { +          for(inti = 0; I < 100; i++) { -System.out.println (Thread.CurrentThread (). GetName () + "" +i); the             if(20==i) { *Secondthread st =NewSecondthread (); $                 NewThread (ST, "Threads 1")). Start ();Panax Notoginseng                 NewThread (ST, "Threads 2")). Start (); -             } the         } +     } A  the}
Secondthread

D, the Runnable object only as the Target,runnable implementation class of the thread object contains the run () method only as the execution of the thread. The actual thread object is still the thread instance, except that the thread thread is responsible for executing its target's run () method.

E, the use of the implementation of the Runnable interface to create multi-threading, thread classes can also inherit other classes, multiple threads can share a target object, to facilitate the processing of multiple threads of the same resource;
F, using the inherited thread class to create multi-threading, if you need to access the current thread, without using the Thread.CurrentThread () method, use this directly to get the current thread.

Multithreading Summary One: Basic concepts

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.