Java Multithreading complete version of the basic knowledge __java

Source: Internet
Author: User
Java Multithreading Complete version of the basic knowledge (Kai Kaishun from thick to thin series) 1. Foreword

Threads are a very important concept in modern operating systems, the multithreading is powerful, the Java language provides good support for threading, and we can easily create multiple threads using the thread class provided by Java. Thread is not difficult, I have studied before the foundation, in this done a collation, this article mainly refers to the Java research organization published J2SE Advanced and Zhang Xiaoxiang:-java Employment Training teaching materials these two books 2. Overview 2.1 Threads what

is mainly the difference between thread and process, no longer elaborated here, self-search online

Why use Threads: the operating system switches multiple threads faster than the scheduling process, can not be shared between processes, communication problems. It is convenient to exchange data between threads because of shared data

Here's an example to explain how multithreading and single-threaded

A single thread example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 package firsttread;    /**  *  @author  zhaikaishun  *  */public class treaddemo1  {    public static void main (String[] args)  {         new testthread (). run ();   //will execute this code all the time          while (true) {             system.out.println ("main thread is running");         }         }     class testthread{  //here does not inherit thread class     public void run () {         while  (true) {         System.out.println ("Testthread is here run");/will always perform          }     } }


appears after running

Testthread is here run

Testthread is here run

Testthread is here run

Testthread is here run

Testthread is here run

...


Analysis: This is a single-threaded, will be in order, will only execute the Testthread class method


B Multithreaded Example

1 2 3 4 5 6 7 8 9 10 11

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.