线程 相关
- 线程 相关 推荐度:
- 相关推荐
线程 相关
守护线程:垃圾回收(GC)线程
线程最大优先级10,最小优先级1,默认优先级5
线程的执行状态:新建、就绪、运行、阻塞、结束
start() 分配资源
run() 执行具体的用户线程代码;不是由用户程序来调用的,当调用start()方法启动一个线程之后,只要线程获得了CPU执行时间,便进入run()方法体执行具体的用户线程
run()为同步执行,失去了线程的意义
线程创建的方法:
一、继承Thread
package com.crazymakercircle.multithread.basic.create;
import personal.nien.javabook.util.Print;
public class CreateDemo {
public static final int MAX_TURN = 5;
public static String getCurThreadName() {
return Thread.currentThread().getName();
}
//线程的编号
static int threadNo = 1;
static class DemoThread extends Thread { //①
public DemoThread() {
super("DemoThread-" + threadNo++); //②
}
public void run() { //③
for (int i = 1; i < MAX_TURN; i++) {
Print.cfo(getName() + ", 轮次:" + i);
}
Print.cfo(getName() + " 运行结束.");
}
}
public static void main(String args[]) throws InterruptedException {
Thread thread = null;
//方法一:使用Thread子类创建和启动线程
for (int i = 0; i < 2; i++) {
thread = new DemoThread();
thread.start();
}
Print.cfo(getCurThreadName() + " 运行结束.");
}
}
二、实现Runnable
Runnable逻辑和数据更好的分离,面向对象,避免单继承带来的局限性
package com.crazymakercircle.multithread.basic.create;
// 省略import
public class SalesDemo
{
public static final int MAX_AMOUNT = 5; //商品数量
//商店商品类(销售线程类),一个商品一个销售线程,每个线程异步销售4次
static class StoreGoods extends Thread
{
StoreGoods(String name)
{
super(name);
}
private int goodsAmount = MAX_AMOUNT;
public void run()
{
for (int i = 0; i <= MAX_AMOUNT; i++)
{
if (this.goodsAmount > 0)
{
Print.cfo(getCurThreadName() + " 卖出一件,还剩:"
+ (--goodsAmount));
sleepMilliSeconds(10);
}
}
Print.cfo(getCurThreadName() + " 运行结束.");
}
}
//商场商品类(target销售线程的目标类),一个商品最多销售4次,可以多人销售
static class MallGoods implements Runnable
{
//多人销售可能导致数据出错,使用原子数据类型保障数据安全
private AtomicInteger goodsAmount = new AtomicInteger(MAX_AMOUNT);
public void run()
{
for (int i = 0; i <= MAX_AMOUNT; i++)
{
if (this.goodsAmount.get() > 0)
{
Print.cfo(getCurThreadName() + " 卖出一件,还剩:"
+ (goodsAmount.decrementAndGet()));
sleepMilliSeconds(10);
}
}
Print.cfo(getCurThreadName() + " 运行结束.");
}
}
public static void main(String args[]) throws InterruptedException
{
Print.hint("商店版本的销售");
for (int i = 1; i <= 2; i++)
{
Thread thread = null;
thread = new StoreGoods("店员-" + i);
thread.start();
}
Thread.sleep(1000);
Print.hint("商场版本的销售");
MallGoods mallGoods = new MallGoods();
for (int i = 1; i <= 2; i++)
{
Thread thread = null;
thread = new Thread(mallGoods, "商场销售员-" + i);
thread.start();
}
Print.cfo(getCurThreadName() + " 运行结束.");
}
}
三、实现Callable
Callable有返回值
/*** Copyright (c) 2022,TravelSky.* All Rights Reserved.* TravelSky CONFIDENTIAL* <p>* Project Name:ldpCoreCompApp* Package Name:PACKAGE_NAME* File Name:Test.java* Date:2022年08月2022/8/2日 10:01:14*/import java.util.concurrent.*;/*** ClassName: Test <br/>* Description: TODO <br/>* Date: 2022年08月2022/8/2日 10:01:14 <br/>* <br/>** @author lixin(邮箱)** 修改记录* @version 产品版本信息 yyyy-mm-dd 姓名(邮箱) 修改信息<br/>**/
public class Test {/*** 继承Thread*/static class TestThread extends Thread{@Overridepublic void run() {System.out.println("TestThread");}}/*** 实现Runnable*/static class TestRunnable implements Runnable{@Overridepublic void run() {System.out.println("TestRunnable");}}/*** 实现Callable*/static class TestCallable implements Callable<String> {@Overridepublic String call() throws Exception {return "TestCallable";}}/*** 线程池创建*/private static ExecutorService executorService = Executors.newFixedThreadPool(3);static class TestExecutor implements Callable<Long> {@Overridepublic Long call() throws Exception {long startTime = System.currentTimeMillis();for (int i = 0; i < 5; i++){System.out.println(Thread.currentThread().getName()+" : "+i);Thread.sleep(100);}long used = System.currentTimeMillis() - startTime;return used;}}public static void main(String[] args) throws ExecutionException, InterruptedException {Thread thread = new TestThread();thread.start();Runnable runnable = new TestRunnable();Thread thread1 = new Thread(runnable);thread1.start();TestCallable testCallable = new TestCallable();FutureTask<String> futureTask = new FutureTask<String>(testCallable);Thread thread2 = new Thread(futureTask);thread2.start();System.out.println(futureTask.get());Future future = executorService.submit(new TestExecutor());System.out.println(future.get());}
}
四、线程池Executor
submit() 接收有返回值和无返回值的入参,有返回值
execute() 只接收无返回值的入参,无返回值
最新文章
- CreateCompatibleDC 解释
- appJSON[tabBar][borderStyle] 字段需为 black 或 white console.error @ VM1402:1 (anonymous) @ VM1415:2
- Excel转Html(一)
- java.lang.NullPointerException: null的错误
- 黑客快速入门
- 什么是 Android Jetpack?
- n皇后问题回溯法
- python,jsonschema, marshmallow, “None is not of type ‘string‘
- AT24C02驱动程序,【I2C串行总线】的组成及工作原理
- 怎么把电脑上的准考证发送到手机上呢
- Hashtable和Hashset
- pgpool连接池
- (Python)异常处理try...except、raise
- 装机、做系统必备:秒懂MBR和GPT分区表
- 浏览器强制刷新
- jdbcDAO
- 计算机软件可以用虚拟串口吗,虚拟串口以及在Proteus中的使用