Multithreading actually in the operating system has been in-depth understanding, now do not do learning concepts, in fact, understand the thread is very simple, the first process is a kernel running software, and the thread is a lightweight process, and the process is different from the thread is generally present in a process, shared address space. Specific courses that can refer to the operating system.
So the Java program running principle and the JVM startup is multithreaded?
The A:java command to start JVM,JVM initiates a process that initiates a main thread.
B:JVM startup is multithreaded because it has a minimum of two threads to boot, the main thread and the garbage collection thread.
The implementation of multithreading is made up of three methods
First Kind
Inherit the thread class and override the Run method
Public Class A extends thread{
@Override
public void Run () {
}
}
Note that when used, we do not call the Run method directly, but instead use start to start the thread.
A a=new a ();
A.start//Not A.run
Describes the methods in several threads, first getting the name: GetName ()//Gets the name of the current process. Method of changing the name a a=new a ("Thread_1")
So think about a problem if it is another thread we want to get a name but not the thread class, such as I want to see the main thread of the method, using GetName, it will error, because the main function is not a thread, there is a way to do this, the thread CurrentThread The () method can return a thread-type parameter, so we can thread CurrentThread (). GetName (). Another method is priority, the knowledge about scheduling is described in both the operating system and the unit. GetPriority () Gets the priority of the current thread. SetPriority () Sets the priority level (up to 10, lowest to 1). Join (), which is the function of having a thread execute and
To execute other threads. Interrupt (), which can cause the thread to break and return a interruptedexpection type, after the error is received, in a try catch in the class, the catch receives an error, executes the contents inside, and then resumes execution, The advantage is that the code can go through the try catch and continue. There are a lot of similar daemon threads and comity threads that you can look at in your own API.
The second way
Implementing the Runnable Interface
Public Class A implements runable{
@Override
public void Run () {
}
}
A a=new a ();
A bit different from the first class, you need to create an object of the thread class and pass the object of the class above as a parameter.
Thread T1=new thread (A, "thread_1");
T1.start ();
So let's compare the two methods first, why do we need two methods? The reason is this, because Java is single-inheritance, that is, if one of my classes has a parent class or may need to inherit later then we use the second method.
It is important to note that the concept of a mutex as learned in the operating system, if the code in run has a high requirement for synchronization, for example: while (tickets>0) {
System.out.println (Thread.currentThread.getName () + "being sold" +tickets--);
}
In multi-threading such code is not safe, such as I have three processes simultaneously into while, assuming that at this time tickets are 1, then there will be a problem, the first process is finished, tickets becomes 0, but the other two processes are still in while, so will continue to execute, Tickets becomes -1.-2. So what should we do about it, the idea here is the same as the idea of the mutex, we make the code allow only one process to enter, then we can solve the problem. What makes Java available is synchronized (), how to use it? Simply put, we will need a piece of code that is only allowed to execute a process at the same time. Note At this point we need to create an object outside of the code as a shared pair image. Then add to this method. As an example:
Object Ob=new object ();
Synchronized (obj) {
while (tickets>0) {
System.out.println (Thread.currentThread.getName () + "being sold" +tickets--);
}
}
Java Learning Day 22nd (about the creation and use of processes)