Java Multithreading--the basic concept and use of multithreading

Source: Internet
Author: User
Tags thread class

I. The basics of processes and threads

1. Concepts of processes and threads

Process: A running application is called a process and has system resources (CPU, memory)

Threads: A piece of code in a process that can have more than one segment of code in a process. does not own resources (resources for shared processes)

In Java, the program portal is automatically created as the main thread, and multiple child threads can be created in the main thread.

Difference:

1, whether the possession of resources

2. The overhead required to create or revoke a process is greater than the overhead required to create or revoke a thread.

3, the process is a heavyweight component, the thread is a lightweight component

Multi-process: multiple tasks (Programs) can be run concurrently in the operating system

Multithreading: Multiple functional streams executing concurrently in the same application

2, the main characteristics of the thread

①, can not exist in a file name in a separate way in the disk;

②, cannot be executed alone, can only be started after the process has been started;

③, threads can share the same memory (code and data) of the process.

3, the main purpose of the thread

①, it can do repetitive work (such as animation, sound, etc.).

②, engaged in one-time more time-consuming initialization work (such as network connection, sound data file loading).

③, concurrent execution effects (multiple threads of a process) for more complex functions

4. Main advantages of multi-threaded (simultaneous running of multiple threads) program

① can alleviate the bottleneck of system performance, because it can operate in parallel;

②, improve the efficiency of CPU processor, in multi-threading, through priority management, can make important program priority operation, improve the flexibility of task management, on the other hand, in multi-CPU system, the different threads can be executed in different CPUs, really do multitasking at the same time.

Second, thread creation and start-up

Actually look at the API, from the thread construction method, you can see how to create a thread:

Thread()
Assigns a new Thread object.
Thread(Runnable target)
Assigns a new Thread object.
Thread(Runnable target,String name)
Assigns a new Thread object.
Thread(String name)
Assigns a new Thread object.
Thread (threadgroup group,runnable  target)
           Assign a new   Thread   object.
Thread (threadgroup group,runnable  target,string name)
           assign a new   Thread   object to   target   as its running object, the specified Span class= "Apple-converted-space" >  name   as its name and as Span class= "Apple-converted-space" >  group   a member of the thread group referenced.
Thread (threadgroup group,runnable  target,string name, long stacksize)
            assign a new   Thread   object in order to   target Span class= "Apple-converted-space" >  as its running object, the specified   name   as its name as   Group   and has the specified stack size .
Thread(ThreadGroup group,String name)
Assigns a new Thread object.

The next 4 construction methods create a thread and join it into a thread group, but the thread is created in a similar way as before.

There are two ways to create a thread in Java:

1. Inherit the thread class, rewrite the Run () method, and then create an instance of a thread directly from the instance of this object. Then call the start () method to start the thread

2. Implement the Runnable interface, rewrite the run () method, and then call new Thread (runnable) to create a thread, and then call the start () method to start the thread

Actually look at the source file of thread, found that it also implements the Runnable interface.

[Java]View plain copy
    1. Public class Thread implements Runnable


1. How to inherit the thread class

[Java]View plain copy
  1. Public class Test1 {
  2. public static void Main (string[] args) {
  3. System.out.println (Thread.CurrentThread (). GetName ());
  4. MyThread mythread=New MyThread ();
  5. Mythread.start ();
  6. }
  7. }
  8. Class MyThread extends thread{
  9. int i=0;
  10. @Override
  11. public Void Run () {
  12. While (i<) {
  13. System.out.println (this.getname () +value of "I" +i);
  14. i++;
  15. }
  16. }
  17. }


2. Implement Runnable interface

[Java]View plain copy
  1. Public class Test1 {
  2. public static void Main (string[] args) {
  3. System.out.println (Thread.CurrentThread (). GetName ());
  4. Thread thread=new Thread (new myrunnable ());
  5. Thread.Start ();
  6. }
  7. }
  8. Class Myrunnable implements runnable{
  9. int i=0;
  10. @Override
  11. public Void Run () {
  12. While (i<) {
  13. System.out.println (Thread.CurrentThread (). GetName () +value of "I" +i);
  14. i++;
  15. }
  16. }
  17. }


Attention:

①, in the way of inheriting thread, you can use the GetName () method to get the name of the current thread, because there is this method in the thread class. However, in the implementation of Runnable mode, but can not use This.getname (), because the Runnable interface does not have this method (can be seen, because we do not have a hint that we need to rewrite this method), So the current thread object can only be obtained by using the thread's static method Thread.CurrentThread (), in the call to the GetName () method, to get the name of the current threading.

②, for Java, the run () method has nothing special. Like the main () method, it is just a new thread that knows the method name (and signature) of the call. Therefore, it is legal to raise the Run method on runnable or thread. But does not start a new thread. The new thread is started only if the start () method is called.

3, two ways of comparison

Multithreading with the implementation of the Runnable interface is advantageous, and is generally used in this way:

1, the thread class just implements the Runnable interface, but also can inherit other classes.

2, in this way, multiple threads can share a runnable target object, so it is very suitable for multiple same threads to handle the same resource situation, so that the CPU, code and data can be separated to form a clear model, better reflect the idea of object-oriented.

Java Multithreading--the basic concept and use of multithreading

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.