Java face question (thread)

Source: Internet
Author: User

The first time to write, write the purpose is to help themselves to better view their knowledge, but also for their usual study of some summary. May be some places are not perfect, coverage of knowledge is not broad enough, please understand.

Question One

The difference between threads and processes:

Processes: Processes are those in the task manager who run the program in the background, and the process is the entity of the program.

Threads: In a full Java program, start execution starts with main, and main is a thread, and Java starts the garbage collection mechanism at the same time from main, and the garbage collection mechanism is another thread. In addition to these two threads, there may be other threads. Complete the execution of a program together with these threads.

Summary: From the above view, the process is the entity of the program, the program has more than one thread to perform, so the thread is inside the process, a process can have multiple threads, the process is the container of the thread.

The thread is at least two: A.main;b. garbage collection mechanism;

Question Two

How to create a thread:

(There is a third way, you can understand it in Baidu itself)

A. Inheriting the Thread class

Inherit the thread, then rewrite the Run method, use the new object directly, and then call the start () method, and the thread is turned on.

B. Implementing the Runnable Interface

Class A implements the Runnable interface, overriding the Run method, using the object A with new out of a, and the object in the new thread with a as its argument. Again through thread

Object calls start () to open the thread.

1  Public classRunnabledemo2 {3      Public Static voidMain (string[] args)4     {5A A =NewA ();6Thread th =NewThread (a);//new Thread (Runnable object);7Th.start ();//Thread Open8 9     }    Ten  One } A  - classAImplementsRunnable - { the      Public voidRun () -     { -System.out.println ("A is running ..."); -     } +}

Summary: Only show the use of runnable, because usually choose to use runnable. Because the use of thread can only be a single inheritance, Runnable can be implemented or inherited can exist, without limitations.

  

Java face question (thread)

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.