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)