First, what is a thread
A thread is an execution unit within a process.
Second, the basic operation of the thread 2.1 State and operation
2.2 New Thread
Thread t1 = new Thread(new CreateThread());t1.start();# 直接覆盖run方法# 传target实例,即Runnable接口实例
2.3 Terminating a thread
2.4 Break Thread
publicvoid Thread.interrupt// 中断线程publicboolean Thread.isInterrupted// 判断是否被中断publicstaticboolean Thread.interrupted// 判断是否被中断,并清除当前中断状态publicstaticnativevoidsleep(longthrows InterruptedException
Code
// 线程t1publicvoidrun() { while(true) { Thread.yield(); }}// 对线程t1进行中断操作,线程t1并不会做出响应t1.interrupt();// 执行下面这段代码的线程,会对中断做出响应publicvoidrun() { while(true) { if (Thread.currentThread().isInterrupted()) { System.out.println("Interrupted!"); break; } Thread.yield(); }}
Sleep Code
//In the process of waiting, also responding to interrupt operation Public void Run() { while(true) {if(Thread.CurrentThread().isinterrupted()) {System. out.println(' interrupted! '); Break; }Try{Thread.Sleep( -); }Catch(Interruptedexception e) {System. out.println("interrupted when Sleep");//Set interrupt status, remove interrupt mark bit after throwing exceptionThread.CurrentThread().Interrupt(); } Thread.yield(); }}
2.5 Suspending and continuing thread execution
Suspend () does not release the lock
If the lock occurs before resume (), a deadlock occurs
These two are not recommended for use by law
To simulate a deadlock:
Public classBadsuspend { Public StaticObject U =NewObject ();StaticChangeobjectthread T1 =New Changeobjectthread("T1");StaticChangeobjectthread t2 =New Changeobjectthread("T2"); Public Static classChangeobjectthreadextendsThread { Public Changeobjectthread(String name) {Super.SetName(name); }@Override Public void Run() {synchronized(u) {System. out.println("in"+GetName()); Thread.CurrentThread().Suspend(); } } } Public Static void Main(string[] args)throwsinterruptedexception {t1.Start(); Thread.Sleep( -); T2.Start(); T1.Resume(); T2.Resume(); T1.Join(); T2.Join(); }}
Analysis:
T1 thread ends normally, T2 thread deadlock
2.6 Waiting for thread to end and humility
Join,yeild
// 把自己占用的CPU机会释放掉,再和别人一起竞争CPUpublicstaticnativevoidyield();// 当前线程未做完,主线程等待当前线程做完后再往下走publicfinalvoidjointhrows InterruptedExceptionpublicfinalsynchronizedvoidjoin(longthrows InterruptedException
Third, the Guardian thread
Silently in the background to complete a number of systematic services, such as garbage collection thread, JIT thread can be understood as a daemon thread
In a Java application, the Java virtual machine naturally exits when only the daemon thread
Four, Thread priority
High-priority threads are more likely to win in the competition
t1.setPriority(Thread.MAX_PRIORITY);
V. Basic thread synchronization Operations 5.1 synchronized
Specifies the lock object: Locks the given object and obtains the lock for the given object before entering the synchronization code.
Directly acting on the instance method: the equivalent of locking the current instance, before entering the synchronization code to obtain the current instance of the lock.
Directly acting on a static method: the equivalent of locking the current class, before entering the synchronization code to obtain the current class lock.
Specifying lock objects
Public classAccountingsyncImplementsRunnable {StaticAccountingsync instance =New Accountingsync();Static inti =0;@Override Public void Run() { for(intj=0;j<100000; j + +) {synchronized(instance) {i++; } } } Public Static void Main(string[] args)throwsinterruptedexception {Thread T1 =NewThread (instance); Thread t2 =NewThread (instance); T1.Start(); T2.Start(); T1.Join(); T2.Join(); System. out.println(i); }}
Acting on an instance method: Notice that multiple threads are locking the same instance
Public classAccountingSync2ImplementsRunnable {StaticACCOUNTINGSYNC2 instance =New ACCOUNTINGSYNC2();Static inti =0; Public synchronized void Increase() {i++; }@Override Public void Run() { for(intj=0;j<100000; j + +) {Increase(); } } Public Static void Main(string[] args)throwsinterruptedexception {Thread T1 =NewThread (instance); Thread t2 =NewThread (instance); T1.Start(); T2.Start(); T1.Join(); T2.Join(); System. out.println(i); }}
Acting on static methods: Pay attention to the difference acting on the instance method
5.2 Wait ()/notify ()
Notify (), allow the thread to go down, but if you do not get the lock, but also can not execute, t2.notifyall () after T1 will be able to execute down, but at this time T1 has not acquired an object lock, must wait for T2 sleep 2 seconds after the object lock to execute.
Java High concurrency-multithreading basics