スレッドの状態
初期化 - レディ - 実行 - 終了
スリープ:タイムアウトは、それがCPUリソースを競争する準備状態になります時間の期間にわたって、待っています。
待っ:待ち状態、いや、我々は、ウェイクを通知したりのnotifyAllて待機する必要があります。
ブロック:ブロックIOやフェイスロックコード、またはロックが動作状態になり得ることがデータを受信すると、それが直接死んだ状態にも可能です。
public class NewThread implements Runnable {
@Override
public synchronized void run() {
while (true) {
System.out.println("线程运行了...");
try {
// Thread.sleep(100);
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//创建线程并执行线程任务
NewThread target = new NewThread();
Thread thread = new Thread(target);
//线程启动
thread.start();
while (true) {
synchronized (target) {
System.out.println("主线程");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
target.notify();
}
}
}
}
スレッドを作成するためのさまざまな方法
Threadクラスの継承
スレッドを作成します。
public class Demo1 extends Thread {
public Demo1(String name) {
super(name);
}
@Override
public void run() {
while (true) {
System.out.println(getName() + "线程执行...");
}
}
public static void main(String[] args) {
Demo1 d1 = new Demo1("first-thread");
Demo1 d2 = new Demo1("second-thread");
//(守护线程) 即使线程没有执行完毕,只要主线程执行完了,线程就会退出
d1.setDaemon(true);
d2.setDaemon(true);
d1.start();
d2.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
スレッドの割り込み:
()だけで無期限に待機するためのスレッド失望させ、ロックやリソースを解放する方法はありませんストップを使用し
、推奨割り込みが()
public class Demo1 extends Thread {
public Demo1(String name) {
super(name);
}
@Override
public void run() {
while (!interrupted()) {
System.out.println(getName() + "线程执行...");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Demo1 d1 = new Demo1("first-thread");
Demo1 d2 = new Demo1("second-thread");
d1.start();
d2.start();
d1.interrupt();
}
}
Runnableを実装
/**
* 作为线程任务存在
*/
public class Demo2 implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("thread running");
}
}
public static void main(String[] args) {
Thread thread = new Thread(new Demo2());
thread.start();
}
}
匿名内部クラスの道
public class Demo3 {
public static void main(String[] args) {
//该线程仅创建一次
new Thread() {
@Override
public void run() {
while (true) {
System.out.println("thread start...");
}
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread start...");
}
}).start();
//执行结果为sub
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("runnable");
}
}) {
@Override
public void run() {
System.out.println("sub");
}
}.start();
}
}
戻り値を持つスレッド
public class Demo4 implements Callable<Integer> {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//执行的任务
Demo4 d = new Demo4();
FutureTask<Integer> task = new FutureTask<Integer>(d);
Thread t = new Thread(task);
t.start();
System.out.println("我先干点别的");
//拿回返回结果
Integer integer = task.get();
System.out.println("线程执行的结果为:" + integer);
}
@Override
public Integer call() throws Exception {
System.out.println("正在进行紧张的计算");
Thread.sleep(3000);
return 1;
}
}
タイマー
public class Demo5 {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//实现定时任务
System.out.println("timertask is run");
}
}, 0, 1000);
}
}
スレッドプールを達成
newFixedThreadPool:
public class Demo6 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(10);
for (int i = 0; i < 100; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
});
}
threadPool.shutdown();
}
}
newCachedThreadPool:
public class Demo6 {
public static void main(String[] args) {
//比较智能的线程池,不够用就创建,够用就回收
ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 100; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
});
}
threadPool.shutdown();
}
}
ラムダ式の実現
public class Demo7 {
public int add(List<Integer> values) {
// values.stream().forEach(System.out::println);
return values.parallelStream().mapToInt(a -> a).sum();
}
public static void main(String[] args) {
List<Integer> values = Arrays.asList(10, 20, 30, 40);
int result = new Demo7().add(values);
System.out.println(result);
}
}
春マルチスレッド
主な方法:
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
DemoService bean = ac.getBean(DemoService.class);
bean.a();
bean.b();
}
}
関連
@Configuration
@ComponentScan("com.autohome.data.realtime.demo")
@EnableAsync
public class Config {
}
非同期コード:
@Service
public class DemoService {
@Async
public void a() {
while (true) {
System.out.println("a");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Async
public void b() {
while (true) {
System.out.println("b");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}