Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions 03concurrency/0301/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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>java0.03concurrency</groupId>
<artifactId>0301</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>
25 changes: 25 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/DaemonThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package java0.conc0301;

public class DaemonThread {

public static void main(String[] args) {
Runnable task = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t = Thread.currentThread();
System.out.println("当前线程:" + t.getName());
}
};
Thread thread = new Thread(task);
thread.setName("test-thread-1");
thread.setDaemon(true);
thread.start();
}


}
11 changes: 11 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/Runner1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package java0.conc0301;

public class Runner1 implements Runnable {

@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("进入Runner1运行状态——————————" + i);
}
}
}
21 changes: 21 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/Runner2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package java0.conc0301;

public class Runner2 implements Runnable {

@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("进入Runner2运行状态——————————" + i);
}

boolean result = Thread.currentThread().isInterrupted();

boolean result1 = Thread.interrupted(); // 重置状态

boolean result3 = Thread.currentThread().isInterrupted();

System.out.println("Runner2.run result ===>" + result);
System.out.println("Runner2.run result1 ===>" + result1);
System.out.println("Runner2.run result3 ===>" + result3);
}
}
25 changes: 25 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/RunnerMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

package java0.conc0301;

public class RunnerMain {

public static void main(String[] args) {

Runner1 runner1 = new Runner1();
Thread thread1 = new Thread(runner1);

Runner2 runner2 = new Runner2();
Thread thread2 = new Thread(runner2);

thread1.start();
thread2.start();

thread2.interrupt();

System.out.println(Thread.activeCount());

// Thread.currentThread().getThreadGroup().list();
// System.out.println(Thread.currentThread().getThreadGroup().getParent().activeGroupCount());
// Thread.currentThread().getThreadGroup().getParent().list();
}
}
14 changes: 14 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/ThreadA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package java0.conc0301;

public class ThreadA extends Thread {

public void run() {
super.run();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("这是线程A");
}
}
26 changes: 26 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/ThreadB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package java0.conc0301;

public class ThreadB implements Runnable {

@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("这是线程B");

Thread currentThread = Thread.currentThread();
String currentThreadName = currentThread.getName();

System.out.println("这是线程的名称:" + currentThreadName);
System.out.println("返回当前线程" + currentThreadName + "的线程组中活动线程的数量:" + Thread.activeCount());
System.out.println("返回该线程" + currentThreadName + "的标识符:" + currentThread.getId());
System.out.println("返回该线程" + currentThreadName + "的优先级:" + currentThread.getPriority());
System.out.println("返回该线程" + currentThreadName + "的状态:" + currentThread.getState());
System.out.println("返回该线程" + currentThreadName + "所属的线程组:" + currentThread.getThreadGroup());
System.out.println("测试该线程" + currentThreadName + "是否处于活跃状态:" + currentThread.isAlive());
System.out.println("测试该线程" + currentThreadName + "是否为守护线程:" + currentThread.isDaemon());
}
}
16 changes: 16 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/ThreadC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package java0.conc0301;

import java.util.concurrent.Callable;

public class ThreadC implements Callable<String> {

@Override
public String call() throws Exception {
Thread.sleep(500);
System.out.println("这是线程C");
return "线程C";
}



}
32 changes: 32 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/ThreadMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package java0.conc0301;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadMain {

public static void main(String[] args) {

ThreadA threadA = new ThreadA();
threadA.start();
System.out.println("这是主线程:");

ThreadB threadB = new ThreadB();
new Thread(threadB).start();
System.out.println("这是主线程:");

ThreadC threadC = new ThreadC();
FutureTask<String> futureTask = new FutureTask<>(threadC);
new Thread(futureTask).start();
System.out.println("这是主线程:begin!");
try {
System.out.println("得到的返回结果是:" + futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

}

}
39 changes: 39 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/ThreadMain2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package java0.conc0301;

public class ThreadMain2 {

public static void main(String[] args) {

ThreadB threadB = new ThreadB();
for (int i = 0; i < 5; i++) {
new Thread(threadB, "线程名称:(" + i + ")").start();
}

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}

//返回对当前正在执行的线程对象的引用
Thread threadMain = Thread.currentThread();
System.out.println("这是主线程:");
System.out.println("返回当前线程组中活动线程的数目:" + Thread.activeCount());
System.out.println("主线程的名称:" + threadMain.getName());
System.out.println("返回该线程的标识符:" + threadMain.getId());
System.out.println("返回线程的优先级:" + threadMain.getPriority());
System.out.println("返回线程的状态:" + threadMain.getState());
System.out.println("返回该线程所属的线程组:" + threadMain.getThreadGroup());
System.out.println("测试线程是否为守护线程:" + threadMain.isDaemon());


// try {
// Thread.sleep(10000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }

}


}
50 changes: 50 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0301/op/Join.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package java0.conc0301.op;

public class Join {

public static void main(String[] args) {
Object oo = new Object();

MyThread thread1 = new MyThread("thread1 -- ");
thread1.setOo(oo);
thread1.start();

synchronized (oo) {
for (int i = 0; i < 100; i++) {
if (i == 20) {
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " -- " + i);
}
}
}

}

class MyThread extends Thread {

private String name;
private Object oo;

public void setOo(Object oo) {
this.oo = oo;
}

public MyThread(String name) {
this.name = name;
}

@Override
public void run() {
synchronized (oo) {
for (int i = 0; i < 100; i++) {
System.out.println(name + i);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package java0.conc0301.op;

public class WaitAndNotify {
public static void main(String[] args) {
MethodClass methodClass = new MethodClass();
Thread t1 = new Thread(() -> {
try {
methodClass.product();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "t1");
Thread t2 = new Thread(() -> {
try {
methodClass.customer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "t2");
Thread t3 = new Thread(() -> {
try {
methodClass.customer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "t3");
t1.start();
t2.start();
t3.start();

}
}

class MethodClass {
// 定义生产最大量
private final int MAX_COUNT = 20;

int productCount = 0;

public synchronized void product() throws InterruptedException {
while (true) {
System.out.println(Thread.currentThread().getName() + ":::run:::" + productCount);
Thread.sleep(10);
if (productCount >= MAX_COUNT) {
System.out.println("货舱已满,,.不必再生产");

wait();
}else {
productCount++;
}

notifyAll();
}
}

public synchronized void customer() throws InterruptedException {
while (true) {
System.out.println(Thread.currentThread().getName() + ":::run:::" + productCount);
Thread.sleep(10);
if (productCount <= 0) {
System.out.println("货舱已无货...无法消费");
wait();
}else {
productCount--;
}

notifyAll();
}
}
}
Loading