Java多线程


# java多线程实现方式

## 方式1:继承Thead,重写run方法

```java

class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++)
System.out.println("thread " + this.getName());
}
}

public class Chap1 {
public static void main(String[] args) {
MyThread th1 = new MyThread();
MyThread th2 = new MyThread();

th1.start();
th2.start();
}
}

```

## 方式2: 实现Runnable接口,重写run方法

```java
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++)
System.out.println("im runnable " + Thread.currentThread().getName());
}
}


public class Chap1 {
public static void main(String[] args) {
Thread th3 = new Thread(new MyRunnable());
Thread th4 = new Thread(new MyRunnable());

th3.start();
th4.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

```

## 方式3: 实现Callable接口,重写call方法

```java
class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
for (int i = 0; i < 5; i++)
System.out.println("this thread " + Thread.currentThread().getName());
return "callable";
}
}

public class Chap1 {
public static void main(String[] args) throws Exception {
MyCallable myCallable = new MyCallable();
FutureTask<String> future = new FutureTask<>(myCallable);

MyCallable myCallable1 = new MyCallable();
FutureTask<String> future1 = new FutureTask<>(myCallable1);

Thread t1 = new Thread(future);
Thread t2 = new Thread(future1);

t1.start();
t2.start();

String getT1 = future.get();
System.out.println(getT1);

TimeUnit.SECONDS.sleep(1);

}
}
```
### 守护线程

```java
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++)
System.out.println("thread " + i + ", " + this.getName());
}
}

class MyThread1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 50; i++)
System.out.println("thread " + i + ", " + this.getName());
}
}


public class Chap1 {
public static void main(String[] args) throws Exception {
MyThread t1 = new MyThread();
MyThread1 t2 = new MyThread1();

t1.setName("thread 1");
t2.setName("守护线程");

// 当普通线程执行完之后,守护线程也就没有继续执行下去的必要
t2.setDaemon(true);

t1.start();
t2.start();
}
}
```

### 线程池

```java
public class MyRunnable implements Runnable{
private String command;

public MyRunnable(String s) {
this.command = s;
}

@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " start, time: " + new Date() + " --> " + command);
processCommand();
System.out.println(Thread.currentThread().getName() + " end, time: " + new Date());
}

private void processCommand() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public String toString() {
return this.command;
}
}
```

```java
public class TraceSourceCode {
private static final int corePoolSize = 5;
public static final int maxPoolSize = 10;
private static final int queueCapacity = 100;
public static final long keepAliceTime = 1L;

public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(
corePoolSize,
maxPoolSize,
keepAliceTime,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(queueCapacity),
new ThreadPoolExecutor.CallerRunsPolicy());

for (int i = 0; i < 10; i++) {
Runnable worker = new MyRunnable("" + i);
executor.execute(worker);
}

executor.shutdown();
while (!executor.isTerminated()) {

}
System.out.println("Finished all threads");
}
}
```

posted @ 2021-05-26 22:27  尘归风  阅读(30)  评论(0编辑  收藏  举报