I. Concepts related to applications and processes in Java
In Java, an application corresponds to a JVM instance (also known as a JVM process), with the general name default Java.exe or Javaw.exe (Windows can be viewed through Task Manager). Java uses a single-threaded programming model in which, in our own programs, only one thread is created, often called the main thread, if the thread is not actively created. Note, however, that while there is only one thread to perform the task, it does not mean that there is only one thread in the JVM, and when the JVM instance is created, many other threads (such as the garbage collector thread) are created.
Because Java uses a single-threaded programming model , it is important to be aware of the need to place time-consuming operations on child threads in UI programming to avoid blocking the main thread (in UI programming, the main thread is the UI thread, which handles user interaction events).
two. How to create a thread in Java
In Java if you want to create a thread, there are generally two ways: 1) inherit the thread class; 2) Implement the Runnable interface.
1. Inheriting the thread class
If you inherit the thread class, you must override the Run method to define the tasks that need to be performed in the Run method.
class MyThread extends Thread { private static int num = 0; public MyThread () {num ++; @Override public void run () {System.out.println ( "actively created" + num + "threads"
Once you have created your own thread class, you can create a thread object and then start the thread with the start () method . Note that instead of calling the run () method to start the thread, the Run method simply defines the task that needs to be performed , and if calling the Run method, which is equivalent to executing the Run method in the main thread, is no different from the normal method call. At this point, a new thread is not created to perform the defined task.
Public classTest { Public Static voidMain (string[] args) {MyThread thread=NewMyThread (); Thread.Start (); }}classMyThreadextendsThread {Private Static intnum = 0; PublicMyThread () {num++; } @Override Public voidrun () {System.out.println ("Active-created" + num + "Threads"); }}
In the code above, a new thread is created by invoking the start () method. To distinguish between the start () method call and the run () method call, consider the following example:
Public classTest { Public Static voidMain (string[] args) {System.out.println ("Main thread ID:" +Thread.CurrentThread (). GetId ()); MyThread Thread1=NewMyThread ("Thread1"); Thread1.start (); MyThread thread2=NewMyThread ("Thread2"); Thread2.run (); }}classMyThreadextendsThread {PrivateString name; PublicMyThread (String name) { This. Name =name; } @Override Public voidrun () {System.out.println ("Name:" + name + "Child thread ID:" +Thread.CurrentThread (). GetId ()); }}
Operation Result:
Main thread id:1name:thread2 child thread ID:1name:thread1 child thread ID:11
The following conclusions can be drawn from the output:
1) The thread IDs of Thread1 and Thread2 are different, and the thread2 and the main thread IDs are the same, stating that the Run method call does not create a new thread , but rather runs the run method directly in the main thread, without any difference from the normal method invocation;
2) Although the Thread1 start method call is called before the Thread2 Run method, the first output is information about the Thread2 Run method call, stating that the process created by the new thread does not block subsequent executions of the main thread.
2. Implement the Runnable interface
Creating Threads in Java In addition to inheriting the thread class, you can implement similar functionality by implementing the Runnable interface. Implementing the Runnable interface must override its Run method.
Here is an example:
Public classTest { Public Static voidMain (string[] args) {System.out.println ("Main thread ID:" +Thread.CurrentThread (). GetId ()); Myrunnable runnable=Newmyrunnable (); Thread Thread=NewThread (runnable); Thread.Start (); }}classMyrunnableImplementsRunnable { Publicmyrunnable () {} @Override Public voidrun () {System.out.println ("Child thread ID:" +Thread.CurrentThread (). GetId ()); }}
runnable Chinese means "task", as the name implies, by implementing the Runnable interface, we define a sub-task, and then leave the subtasks to thread to execute . Note that this method must use runnable as the parameter of the thread class, and then create a new thread to execute the subtask through the start () method of the thread. If you call Runnable's Run method, you will not create a new thread, which is no different from a normal method call.
In fact, looking at the implementation source code of the thread class will find that the thread class implements the Runnable interface.
In Java, these 2 ways can be used to create threads to perform subtasks, depending on which way to look at their own needs. Inheriting the thread class directly may seem more concise than implementing the Runnable interface, but since Java allows only single inheritance, you can only choose to implement the Runnable interface if the custom class needs to inherit other classes.
Reference:
Http://www.cnblogs.com/dolphin0520/p/3913517.html
Java concurrency Programming: How to create Threads