转换图片路径为相对路径

This commit is contained in:
罗祥
2019-11-14 17:45:58 +08:00
parent e266780b49
commit 2ffc0a3a94
29 changed files with 342 additions and 89 deletions

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.heibaiying</groupId>
<artifactId>java-concurrency</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,17 @@
package com.heibaiying.interrupt;
/**
* interrupt() 只是设置中断标志位,并不能中断线程
*/
public class J1_Interrupt {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true) {
System.out.println("子线程打印");
}
});
thread.start();
Thread.sleep(10);
thread.interrupt();
}
}

View File

@ -0,0 +1,18 @@
package com.heibaiying.interrupt;
/**
* isInterrupted() 用于检查当前线程是否存在中断标志位
*/
public class J2_IsInterrupted {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("子线程打印");
}
});
thread.start();
Thread.sleep(10);
thread.interrupt();
}
}

View File

@ -0,0 +1,18 @@
package com.heibaiying.interrupt;
/**
* 判断当前线程的中断状态,并清除当前线程的中断标志位
*/
public class J3_Interrupted {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (!Thread.interrupted() || !Thread.currentThread().isInterrupted()) {
System.out.println("子线程打印");
}
});
thread.start();
Thread.sleep(10);
thread.interrupt();
}
}

View File

@ -0,0 +1,22 @@
package com.heibaiying.waitAndNotify;
/**
* 正常情况下
*/
public class J1_Normal {
private static final Object object = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (object) {
System.out.println("线程1");
}
}).start();
new Thread(() -> {
synchronized (object) {
System.out.println("线程2");
}
}).start();
}
}

View File

@ -0,0 +1,29 @@
package com.heibaiying.waitAndNotify;
/**
* 锁住对象不释放
*/
public class J2_KeepObjectLocked {
private static final Object object = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (object) {
while (true) {
try {
Thread.sleep(1000);
System.out.println("线程1");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(() -> {
synchronized (object) {
System.out.println("线程2");
}
}).start();
}
}

View File

@ -0,0 +1,31 @@
package com.heibaiying.waitAndNotify;
/**
* 等待与唤醒
*/
public class J3_WaitAndNotify {
private static final Object object = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (object) {
try {
System.out.println("对象object等待");
object.wait();
System.out.println("线程1后续操作");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
synchronized (object) {
System.out.println("对象object唤醒");
object.notify();
System.out.println("线程2后续操作");
}
}).start();
}
}

View File

@ -0,0 +1,52 @@
package com.heibaiying.waitAndNotify;
/**
* 利用 wait 和 Notify 实现交替打印
*/
public class J4_AlternatePrinting {
private static final Object object = new Object();
private volatile static boolean flag = false;
private static int i = 0;
private static int threshold = 1000000;
public static void main(String[] args) {
new Thread(() -> {
synchronized (object) {
try {
while (i <= threshold) {
if (flag) {
object.wait();
} else {
object.notify();
System.out.println("Thread 1 : " + i++);
flag = !flag;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
synchronized (object) {
while (i <= threshold) {
try {
while (i <= threshold) {
if (flag) {
object.notify();
System.out.println("Thread 2 : " + i++);
flag = !flag;
} else {
object.wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}

View File

@ -0,0 +1,43 @@
package com.heibaiying.waitAndNotify;
/**
* 全部唤醒
*/
public class J5_NotifyAll {
private static final Object object = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (object) {
try {
System.out.println("对象object在线程1等待");
object.wait();
System.out.println("线程1后续操作");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
synchronized (object) {
try {
System.out.println("对象object在线程2等待");
object.wait();
System.out.println("线程2后续操作");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
synchronized (object) {
System.out.println("对象object唤醒");
// 如果是object.notify()则是随机唤醒任意一个等待
object.notifyAll();
System.out.println("线程3后续操作");
}
}).start();
}
}