feat: setDaemon
This commit is contained in:
parent
71ffb918f6
commit
c334d09d65
49
pom.xml
49
pom.xml
|
@ -26,32 +26,25 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.36</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>2.0.16</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>2.0.16</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,22 @@
|
|||
package feture.feture;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
public class MyFutureTask {
|
||||
public static void main(String[] args) {
|
||||
FutureTask<Integer> futureTask = new FutureTask<>(new Callable<>() {
|
||||
|
||||
/**
|
||||
* Computes a result, or throws an exception if unable to do so.
|
||||
*
|
||||
* @return computed result
|
||||
* @throws Exception if unable to compute a result
|
||||
*/
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package feture.feture_2;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
public class MyFutureTask {
|
||||
public static void main(String[] args) {
|
||||
FutureTask<Integer> futureTask = new FutureTask<>(new Callable<>() {
|
||||
|
||||
/**
|
||||
* Computes a result, or throws an exception if unable to do so.
|
||||
*
|
||||
* @return computed result
|
||||
* @throws Exception if unable to compute a result
|
||||
*/
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
package thead.feture;
|
||||
|
||||
public class FutureTask {
|
||||
}
|
|
@ -2,12 +2,20 @@ package thead.thread_4;
|
|||
|
||||
public class Application {
|
||||
// 有线程安全的操作
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
MyThread t1 = new MyThread("t1");
|
||||
MyThread t2 = new MyThread("t2");
|
||||
MyThread t3 = new MyThread("t3");
|
||||
System.out.println("是否中断:" + t1.isInterrupted());
|
||||
|
||||
t1.start();
|
||||
t2.start();
|
||||
t3.start();
|
||||
|
||||
Thread.sleep(100);
|
||||
System.out.println("是否中断:" + t1.isInterrupted());
|
||||
|
||||
t1.interrupt();
|
||||
System.out.println("是否中断:" + t1.isInterrupted());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package thead.thread_5;
|
||||
|
||||
public class Application3 {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
MyThread thread = new MyThread();
|
||||
thread.start();
|
||||
Thread.sleep(100);
|
||||
thread.interrupt();
|
||||
System.out.println("是否中断1:" + thread.isInterrupted());
|
||||
System.out.println("是否中断2:" + thread.isInterrupted());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package thead.thread_5;
|
||||
|
||||
public class MyThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
super.run();
|
||||
for (int i = 0; i < 500000; i++) {
|
||||
System.out.println("i=" + (i + 1));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package thead.thread_5;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class MyThreadTest1 {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
Thread thread = new Thread("t1") {
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void run() {
|
||||
super.run();
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
};
|
||||
|
||||
thread.start();
|
||||
log.info("t1:{}", thread.getState());
|
||||
|
||||
Thread.sleep(500);
|
||||
log.info("t1-2:{}", thread.getState());
|
||||
|
||||
thread.setDaemon(true);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue