Skip to content

Commit

Permalink
auto commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CyC2018 committed Apr 7, 2018
1 parent 979ab46 commit 5735171
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions notes/Java 并发.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ public class MyRunnable implements Runnable {
public void run() {
// ...
}
public static void main(String[] args) {
MyRunnable instance = new MyRunnable();
Tread thread = new Thread(instance);
thread.start();
}
}
```

```java
public static void main(String[] args) {
MyRunnable instance = new MyRunnable();
Thread thread = new Thread(instance);
thread.start();
}
```

Expand All @@ -68,17 +71,21 @@ public class MyRunnable implements Runnable {
public class MyCallable implements Callable<Integer> {
public Integer call() {
// ...
}
public static void main(String[] args) {
MyCallable mc = new MyCallable();
FutureTask<Integer> ft = new FutureTask<>(mc);
Thread thread = new Thread(ft);
thread.start();
System.out.println(ft.get());
return 123;
}
}
```

```java
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable mc = new MyCallable();
FutureTask<Integer> ft = new FutureTask<>(mc);
Thread thread = new Thread(ft);
thread.start();
System.out.println(ft.get());
}
```

## 继承 Thread 类

同样也是需要实现 run() 方法,并且最后也是调用 start() 方法来启动线程。
Expand All @@ -88,10 +95,13 @@ public class MyThread extends Thread {
public void run() {
// ...
}
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
}
}
```

```java
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
}
```

Expand Down Expand Up @@ -161,7 +171,7 @@ main() 属于非后台线程。
3. 等待某个 I/O 的完成;
4. 试图在某个对象上调用其同步控制方法,但是对象锁不可用,因为另一个线程已经获得了这个锁。

**阻塞 睡眠 挂起**
**阻塞 / 睡眠 / 挂起**

阻塞是一种状态,而睡眠和挂起是一种手段,通过睡眠和挂起可以让一个线程进入阻塞状态。

Expand Down

0 comments on commit 5735171

Please sign in to comment.