The basic method of java multithreading
Thread sleep sleep ()
We can sleep method to set the thread to sleep, you can see that sleep is a static method.
public static native void sleep(long var0) throws InterruptedException;
try {
System.out.println(new Date().getSeconds());
Thread.sleep(5000);
System.out.println(new Date().getSeconds());
} catch (InterruptedException e) {
e.printStackTrace ();
}
setDaemon daemon thread
Non-daemon thread to stop, then automatically exit daemon thread
public static void main(String[] args) {
Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 5; i ++) {
System.out.println ( "non-daemon thread");
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 200; i ++) {
System.out.println ( "daemon thread");
}
}
};
thread2.setDaemon(true);
thread1.start();
thread2.start();
}
The program thread2 only five stops output, in fact it should output 200 times, this is because when the thread is finished after, thread as a daemon thread is automatically stopped.
Multithreading join
If you perform a join method, then the current thread to stop, start and execute the join () thread. Equivalent to jump the queue to perform. As follows, in the implementation of thread2 thread, if i == 20, when the first execution let thread1 jump the queue.
public static void main(String[] args) {
final Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 500; i ++) {
System.out.println("thread1---" + i);
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 200; i ++) {
if (i == 20) {
try {
// execution to jump the queue
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace ();
}
}
System.out.println(i);
}
}
};
thread1.start();
thread2.start();
}
join () method may also pass parameters ms long join (ms)
Let's join represents a thread of execution, execution XXX milliseconds to jump the queue, over time, two threads execute alternately
public static void main(String[] args) {
final Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 500; i ++) {
System.out.println("thread1---" + i);
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 200; i ++) {
if (i == 20) {
try {
// 1 millisecond execution to jump the queue
thread1.join(1);
} catch (InterruptedException e) {
e.printStackTrace ();
}
}
System.out.println(i);
}
}
};
thread1.start();
thread2.start();
}
Yeild comity thread
yeild make the cpu, so that other threads
public static void main(String[] args) {
final Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 500; i ++) {
System.out.println( getName() + "---" + i);
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 200; i ++) {
if (i % 5 == 0) {
Thread.yield();
}
System.out.println(getName() + "---" + i);
}
}
};
thread1.start();
thread2.start();
}
setpriority to set thread priority
The default priority is 5, minimum 1, maximum 10, the greater the higher priority
public static void main(String[] args) {
final Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 500; i ++) {
System.out.println( getName() + "---" + i);
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 500; i ++) {
System.out.println(getName() + "---" + i);
}
}
};
// set the maximum thread priority up to 10
thread1.setPriority(Thread.MIN_PRIORITY);
// set the minimum thread priority, minimum 1
thread2.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
}
sync block synchronized
When the multi-threaded, multi-segment code executed simultaneously when. We hope in the implementation of the code which, cpu not switch threads
Synchronization method
Synchronization method refers to the method lock on
The synchronization method of static objects is an object of the class bytecode
Non-static method of synchronization lock object is this
public class ThreadSynchroniedMethod {
public static void main(String[] args) {
final Say say = new Say();
Thread thread1 = new Thread() {
@Override
public void run() {
for (int i = 0 ; i < 10000 ; i ++) {
say.say();
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for (int i = 0 ; i < 10000 ; i ++) {
say.say1();
}
}
};
// set the maximum thread priority up to 10
thread1.setPriority(Thread.MIN_PRIORITY);
// set the minimum thread priority, minimum 1
thread2.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
}
}
class Say {
// lock on method
static synchronized void say() {
System.out.print("s ");
System.out.print("a ");
System.out.print("y ");
System.out.print("h ");
System.out.print("e ");
System.out.print("l ");
System.out.print("l ");
System.out.println("o");
}
static void say1() {
synchronized (Say.class) {
System.out.print("1 ");
System.out.print("2 ");
System.out.print("3 ");
System.out.print("4 ");
System.out.print("5 ");
System.out.print("6 ");
System.out.print("7 ");
System.out.println("8");
}
}
}
Multiple threads using the same resource lock easily lead to deadlocks
Deadlock refers to two or more processes in the implementation process, due to the competition for resources or A blocking phenomenon caused due communicate with each other, without external force, they will not be able to promote it. At this time, say the system is in deadlock state or system to produce a deadlock, which is always in the process of waiting for another process called the deadlock.
Thread-safe class
Vector
StringBuffer
HashTable
Thread safe
ArrayList
StringBuilder
HashSet
There java.util.Collections synchronizedList and other methods, we support the unsafe set of threads turned into thread-safe while learning, we know that many times it is illegal to start