06- idea of how to achieve multi-threaded and multi-threaded mode 1
- Demand: we want to achieve multi-threaded programs.
How to achieve it?
Since the thread is dependent on the process exist, so we should first create out of a process.
The process is created by the system, so we should go call the system functions to create a process.
Java can not call the system function directly, so we have no way to directly implement multi-threaded programs.
But it? Java can go to call C / C + + written procedures to achieve multi-threaded program.
Then provides classes for our use. We can achieve a multi-threaded program.
So what Java provides classes is it?
Thread
By looking at the API, we know that there are 2 ways to achieve multi-threaded programs.
- Option 1: Thread class inheritance.
Step
A: MyThread custom classes inherit the Thread class.
B: MyThread class which override the run ()?
Why is the run () method do?
C: Create Object
D: starting a thread
07- 1 multi-thread code to achieve
- This class should override the run () method, and why?
All code is not in the class need to be a thread of execution.
And this time, in order to be able to distinguish what a thread of execution of code, java provides a Thread class run () is used to code those containing a thread of execution.
- Why call the run () method is single-threaded it?
Because the run () method called directly in fact equivalent to an ordinary method calls, so you're seeing is the effect of single-threaded
in order to see the effect of multi-threaded, it is necessary to talk about another way: start ()
- Interview questions: the difference between run () and start () are?
run (): the code packages are simply a thread of execution, a direct call is a common method
start (): started first thread , then the thread jvm to calling the run () method.
08- getting and setting thread object name
How to get the name of the thread object it?
- public final String getName (): Get the name of the thread.
How to set the name of the thread object it?
- Set the name of the thread: public final void setName (String name)
How to obtain the thread object name for it is not a subclass of the Thread class?
- public static Thread currentThread (): Returns the currently executing thread object
- Thread. currentThread() . getName()|
eg: I want to get the name of the main thread object method is located, how to do it?
System.out.println (Thread. currentThread() .getName () ) ;
09- thread scheduling and getting and setting thread priorities
How to get priority thread object?
- public final int getPriority (): Returns the thread object's priority
How to set priority thread object of it?
- public final void setPriority (int newPriority): Changing thread priority concern.
note:
The default thread priority is 5. The
thread priorities are: 1-10
thread with higher priority only indicates a high probability thread gets CPU time slice , but more to the number of times, or when multiple runs to see fairly good results.
- IllegalArgumentException: illegal argument exception.
Thrown to indicate that passed an illegal or incorrect parameter to the method.