Multithreading in Java and Java Multithreading
1. Main methods in the thread
A) isAlive () determines whether the thread is still alive, that is, whether the thread is not terminated
B) getPriority () obtains the thread priority.
C) setPriority () sets the thread priority.
D) Thread. sleep () sets the Thread sleep time.
E) jion () combines the current thread with this thread
F) yield () for outgoing CUP
G) thread priority
I. Thread. MIN_PRIORITY = 1 minimum priority
Ii. Thread. NORM_PRIORITY = 5 default priority
Iii. Thread. MAX_PRIORITY = 10 maximum priority
2. Thread interruption
A) Stop () is out of date, basically not needed
B) Interrupt () is simple and crude.
C) we recommend that you set the flag.
3. Advanced thread operations
A) wait () waits for the current thread until it is awakened by its thread
B) y () Wake up the waiting thread
4. Two synchronous methods (mainly using synchronized)
A) lock code block
I. Synchronized (object) {code block to lock}
B) Lock method (recommended)
I. Synchronized void method (){}
1. There are two main methods to implement Java multithreading: one is to inherit the Thread class and the other is to implement the Runnable interface. When multithreading is used, two methods are used. One is to overwrite the run () method to implement the code to be executed. The second method is start (), which is used to start the thread.
2. Implementation of the Thread class
1 public class ThreadDemo extends Thread {2 3 public void run () {4 for (int I = 0; I <20; I ++) {5 System. out. println (Thread. currentThread (). getName () + I); 6} 7} 8 public static void main (String [] args) {9 10 ThreadDemo td1 = new ThreadDemo (); 11 td1.setName ("thread 1: "); 12 13 ThreadDemo td2 = new ThreadDemo (); 14 td2.setName (" thread 2: "); 15 16 // get priority 17 System. out. println ("the priority of thread 1 is:" + td1.getPriority (); 18 19 // set the priority of the thread to 0-10 from small to large 20 td1.setPriority (MIN_PRIORITY ); 21 td2.setPriority (MAX_PRIORITY); 22 23 td1.start (); 24 td2.start (); 25 26 27} 28 29}
1. Implementation of the Runnable interface
1 ublic static void main (String [] args) {2 TicketRunnable tr = new TicketRunnable (); 3 4 Thread td1 = new Thread (tr ); 5 td1.setName ("Window 1"); 6 Thread td2 = new Thread (tr); 7 td2.setName ("window 2"); 8 Thread td3 = new Thread (tr ); 9 td3.setName ("window 3"); 10 11 td1.start (); 12 td2.start (); 13 td3.start (); 14}
Multithreading in java
Statements in the run method of your class AddThread
For (int I = start; I <count; I ++)
There is a problem.
Analysis: When you pass in for the first time, start = 0. After the second thread is created, start of the second thread is 10000. However, your count is a fixed value of 10000, the second thread almost stops running. This is also the case for other threads later. Instead of not executing, It is not executing the content in the for loop.
Modify the statement:
For (int I = start; I <count + start; I ++)
You can find that other threads execute the content in the for loop.
Why is multithreading in java?
All of the following are for my personal understanding.
1. Image metaphor:
In any programming language, such as JAVA
System. out. println ("first thread ");
System. out. println ("second thread ");
System. out. println ("third thread ");
The three rows are sequential and run from top to bottom.
If multithreading is used (the code is too large to be pasted out), the above three lines can run simultaneously without queuing up one line.
2. Official concepts
In SUN's JAVA Tutorial, multithreading is Multi-Threading in Concurrency (the Thread is translated as a Thread ). Thread, like Process, but different from Process (Process is a program, and all the programs in the Task Manager obtained by ctrl + alt + del belong to Process ). Process charges memory, CPU fees, and other resources, and Thread can be said to be a lightweight Process. There must be at least one Thread in a Process, and there may be multiple threads.
3. Practical Application
A program with a Client-Server Model (Client-Server. It may take a long time for the server to communicate with a customer (such as user login, it takes time to query the database ). Let all other customers wait? No. Therefore, a Thread is prepared for each customer so that all customers can communicate with the server at the same time without interfering with each other.
4. Difficulties
Thread has advantages and disadvantages. There are many problems with threads in applications. programmers must pay attention to them. For example, Thread Interference (two threads interfere with each other)
Memory Consistency Errors (Memory inconsistency)
Deadlock)
Starvation and Livelock (active)
There are solutions to these problems, and the code is too much to be pasted. Please refer to the reference materials!
Reference: java.sun.com/...d.html