当前位置: 首页 > news >正文

网站宣传的传统方式有哪些站长工具关键词查询

网站宣传的传统方式有哪些,站长工具关键词查询,独立网站做外贸报价,益阳网站建设Java线程 文章目录 Java线程一、线程创建二、线程运行三、线程运行四、主线程和守护线程五、线程的五种状态六、线程的六种状态七、烧水泡茶案例 一、线程创建 创建线程方法一: Thread重写run方法 Slf4j(topic "c.MyTest1") public class MyTest1 {publ…

Java线程

`

文章目录

  • Java线程
  • 一、线程创建
  • 二、线程运行
  • 三、线程运行
  • 四、主线程和守护线程
  • 五、线程的五种状态
  • 六、线程的六种状态
  • 七、烧水泡茶案例


一、线程创建

创建线程方法一:
Thread重写run方法
@Slf4j(topic = "c.MyTest1")
public class MyTest1 {public static void main(String[] args) {Thread t=new Thread(){@Overridepublic void run() {log.info("好人");}};t.setName("线程1");t.start();log.info("main测试");}
}创建方法2:
直接写一个Runable传给线程Thread
@Slf4j(topic = "c.Test2")
public class Test2 {public static void main(String[] args) {Runnable r = () -> {log.debug("running");};Thread t = new Thread(r, "t2");t.start();}
}

Thread和Runable的区别
Thread实现了Runable方法,也就是说Thread可以覆盖Runable的任务方法执行run。但是也可以通过传入任务来执行对应的Runable方法(如下面代码所示)。

 @Overridepublic void run() {if (target != null) {target.run();}
}
创建方法3:
FutureTask实现了RunnableFutureFuture主要是用来返回值的。其实相当于也是一个任务类但是需要实现Callable实现类对象传入到task上。并且执行之后才能够使用task的get来获取数据,如果没有执行线程那么get就会阻塞
@Slf4j(topic = "c.MyTest2")
public class MyTest2 {public static void main(String[] args) throws ExecutionException, InterruptedException {FutureTask<Integer> task=new FutureTask<Integer>(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {log.debug("你好");return 4;}});Thread t=new Thread(task,"t1");t.start();log.debug("{}",task.get());}
}

总结:更推荐方法2,原因是Runable就是任务,把任务和线程分开才能更好的分配任务。

二、线程运行

命令
jps:查看java进程
tasklist:windos查看所有进程
taskkill /F /PID XXX:杀死某个进程

三、线程运行

线程的栈
每次开启一个线程都会产生一个线程的栈给线程使用,实际上就是一开始分配的虚拟机栈。
每个栈都有多个栈帧
线程只能有一个活动栈帧

线程执行的过程
先分配栈给线程,线程调用main方法,在栈分配一个栈帧给main方法,栈帧保存锁记录、局部变量表、操作数栈、返回地址(返回到原来的栈帧方法的下一条指令)

在这里插入图片描述
线程上下文切换

导致上下文切换的条件:
时间片用完
优先级
垃圾回收
自己调用sleep、wait等

线程的状态包括:
程序计数器,记录执行到什么位置
虚拟机栈,包括所有栈帧信息

常见方法
start
线程进入就绪状态,等待调度器调用。这里相当于是开启了一个新的线程
run
执行Runnable里面的方法。这里并没有开启线程,只是通过本线程执行代码
如果开启了两次start就会出现线程状态异常的问题IlegalThreadStateException
线程的两个状态
NEW
start之后就是Runnable等待调度
sleep
线程睡眠,并把状态改为Timed Waiting,被打断的时候会抛出异常InterruptedException。可以通过TimeUnit.SECONDS.sleep来规定睡眠时间的单位。睡眠可以使用在while循环自转的地方,如果长时间自转就会消耗CPU的使用时间,其他线程无法使用
interrupt
唤醒线程,如果线程处于睡眠状态那么就会抛出异常

public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread("t1") {@Overridepublic void run() {log.debug("enter sleep...");try {Thread.sleep(2000);} catch (InterruptedException e) {log.debug("wake up...");e.printStackTrace();}}};t1.start();Thread.sleep(1000);log.debug("interrupt...");t1.interrupt();}

yield
其实就是把线程状态从Running转变到Runnable暂时让出cpu,重新去竞争
setPriority
设置优先级是给调度器进行提示,先执行这个线程,但是仍然没有办法控制线程。
join
join实际上就是卡点,就是一定要等待调用join的线程完成后才能执行下面的代码

@Slf4j(topic = "c.Test10")
public class Test10 {static int r = 0;public static void main(String[] args) throws InterruptedException {test1();}private static void test1() throws InterruptedException {log.debug("开始");Thread t1 = new Thread(() -> {log.debug("开始");sleep(1);log.debug("结束");r = 10;},"t1");t1.start();t1.join();log.debug("结果为:{}", r);log.debug("结束");}
}

同步应用案例
多线程的join只需要等待最长的那个线程就可以了,因为它们是并行执行的。
test3案例,实际上就是限时等待,如果超过时间那么就不等了。如果没有超过时间,那么结束join还是以处理完线程的任务时间为主,而不是最大的等待时间
在这里插入图片描述

@Slf4j(topic = "c.TestJoin")
public class TestJoin {static int r = 0;static int r1 = 0;static int r2 = 0;public static void main(String[] args) throws InterruptedException {test2();}public static void test3() throws InterruptedException {Thread t1 = new Thread(() -> {sleep(2);r1 = 10;});long start = System.currentTimeMillis();t1.start();// 线程执行结束会导致 join 结束log.debug("join begin");t1.join(3000);long end = System.currentTimeMillis();log.debug("r1: {} r2: {} cost: {}", r1, r2, end - start);}private static void test2() throws InterruptedException {Thread t1 = new Thread(() -> {sleep(1);r1 = 10;});Thread t2 = new Thread(() -> {sleep(2);r2 = 20;});t1.start();t2.start();long start = System.currentTimeMillis();log.debug("join begin");t1.join();log.debug("t1 join end");t2.join();log.debug("t2 join end");long end = System.currentTimeMillis();log.debug("r1: {} r2: {} cost: {}", r1, r2, end - start);}private static void test1() throws InterruptedException {log.debug("开始");Thread t1 = new Thread(() -> {log.debug("开始");sleep(1);log.debug("结束");r = 10;});t1.start();t1.join();log.debug("结果为:{}", r);log.debug("结束");}
}

在这里插入图片描述
interrupt
打断阻塞
打断sleep和wait。打断后会抛出异常到那时不会给它打上打断标记。而且这里需要给主线程睡眠一会,不然t1线程还没睡眠,主线程就已经调用打断,那么这个时候的打断是打断t1,并且加上打断标记,但是打断睡眠并不会有打断标记

@Slf4j(topic = "c.Test11")
public class Test11 {public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {log.debug("sleep...");try {Thread.sleep(5000); // wait, join} catch (InterruptedException e) {e.printStackTrace();}},"t1");t1.start();Thread.sleep(1000);log.debug("interrupt");t1.interrupt();log.debug("打断标记:{}", t1.isInterrupted());}
}

打断正常
当我们打断的事正常的线程(没在sleep或wait),那么就会给这个线程加上打断标记。但是要不要打断取决于被打断线程的意愿,其它线程只能给一个通知。

@Slf4j(topic = "c.Test12")
public class Test12 {public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {while(true) {boolean interrupted = Thread.currentThread().isInterrupted();if(interrupted){break;}}}, "t1");t1.start();Thread.sleep(1000);log.debug("interrupt");t1.interrupt();}
}

LockSupport
其实就是一个锁的支持类,它的park方法可以模拟sleep把线程进行阻塞,但是需要标记是false的时候。如果打断标记是true那么就无法使用。但是这个地方可以使用Thread.interrupted来获取打断标记状态和消除标记。

public static void test5() {Thread t1 = new Thread(()->{log.debug("park...");LockSupport.park();log.debug("unpark...");log.debug("打断状态:{}", Thread.interrupted());LockSupport.park();log.debug("unpark...");},"t1");t1.start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}t1.interrupt();}

四、主线程和守护线程

守护线程其实就是Daemon。也就是在其它非守护线程运行完之后,无论守护线程是否还有任务需要执行都会强制停止。
垃圾回收器是守护线程
tomcat的Acceptor和poller

@Slf4j(topic = "c.Test15")
public class Test15 {public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {while (true) {if (Thread.currentThread().isInterrupted()) {break;}}log.debug("结束");}, "t1");t1.setDaemon(true);t1.start();Thread.sleep(1000);log.debug("结束");}
}

五、线程的五种状态

1、初始:new线程的时候
2、可运行:执行了start
3、运行:线程可以使用cpu的时候
4、阻塞:不能被cpu调度器使用的时候
5、终止:线程结束的时候
在这里插入图片描述

六、线程的六种状态

NEW:初始化
RUNNABLE:包括了运行、可运行和阻塞,通常表示正在运行
WAITING:没有时间限制的等待
TIMED_WAITING:有时间限制的等待
BLOCKED:阻塞
TERMINATED:终止

七、烧水泡茶案例

join思路
其实就是老王洗烧壶和烧水,小王洗茶壶、茶杯、茶叶,等待烧水完成之后泡茶。可以用join来等待t1处理。

Slf4j(topic = "c.Test16")
public class Test16 {public static void main(String[] args) {Thread t1 = new Thread(() -> {log.debug("洗茶壶");Sleeper.sleep(1);log.debug("烧水");Sleeper.sleep(5);}, "老王");Thread t2=new Thread(()->{log.debug("洗茶壶");Sleeper.sleep(1);log.debug("洗茶叶");Sleeper.sleep(2);log.debug("洗茶杯");Sleeper.sleep(1);try {t1.join();} catch (InterruptedException e) {e.printStackTrace();}log.debug("泡茶");},"小王");t1.start();t2.start();}
}


文章转载自:
http://accessit.fcxt.cn
http://advert.fcxt.cn
http://indenture.fcxt.cn
http://energise.fcxt.cn
http://crossbowman.fcxt.cn
http://allantois.fcxt.cn
http://potentially.fcxt.cn
http://eloign.fcxt.cn
http://tartrate.fcxt.cn
http://husky.fcxt.cn
http://auburn.fcxt.cn
http://xiv.fcxt.cn
http://teleost.fcxt.cn
http://excuse.fcxt.cn
http://linkwork.fcxt.cn
http://lockpick.fcxt.cn
http://manometry.fcxt.cn
http://aspersion.fcxt.cn
http://phosphorism.fcxt.cn
http://mirdita.fcxt.cn
http://office.fcxt.cn
http://sum.fcxt.cn
http://diandrous.fcxt.cn
http://laminectomy.fcxt.cn
http://ganglioid.fcxt.cn
http://krewe.fcxt.cn
http://sonolysis.fcxt.cn
http://vibrograph.fcxt.cn
http://appeasement.fcxt.cn
http://sacerdotal.fcxt.cn
http://cattleman.fcxt.cn
http://triclinic.fcxt.cn
http://gower.fcxt.cn
http://phyllotactical.fcxt.cn
http://subcutaneously.fcxt.cn
http://reunion.fcxt.cn
http://coprophobic.fcxt.cn
http://lactonic.fcxt.cn
http://pharynges.fcxt.cn
http://wrestler.fcxt.cn
http://protochordate.fcxt.cn
http://anticly.fcxt.cn
http://flatiron.fcxt.cn
http://cortex.fcxt.cn
http://cybernetician.fcxt.cn
http://pretensive.fcxt.cn
http://explosively.fcxt.cn
http://pillage.fcxt.cn
http://curie.fcxt.cn
http://allotransplant.fcxt.cn
http://bonhomous.fcxt.cn
http://impressional.fcxt.cn
http://quantifier.fcxt.cn
http://offering.fcxt.cn
http://embarrassment.fcxt.cn
http://sovnarkhoz.fcxt.cn
http://sever.fcxt.cn
http://marrowbone.fcxt.cn
http://stradivari.fcxt.cn
http://appulsive.fcxt.cn
http://simultaneously.fcxt.cn
http://ethnologic.fcxt.cn
http://stannary.fcxt.cn
http://kinesthesis.fcxt.cn
http://unrecognized.fcxt.cn
http://recovery.fcxt.cn
http://mirthquake.fcxt.cn
http://acanthus.fcxt.cn
http://mumblingly.fcxt.cn
http://flashy.fcxt.cn
http://blackfellow.fcxt.cn
http://justle.fcxt.cn
http://repeal.fcxt.cn
http://barnstorm.fcxt.cn
http://scarcely.fcxt.cn
http://teleradiography.fcxt.cn
http://hyperparasitic.fcxt.cn
http://hyperparathyroidism.fcxt.cn
http://polyglotter.fcxt.cn
http://whistle.fcxt.cn
http://cryoconite.fcxt.cn
http://earpiece.fcxt.cn
http://endeavor.fcxt.cn
http://tolerance.fcxt.cn
http://perinatology.fcxt.cn
http://mazel.fcxt.cn
http://magnesite.fcxt.cn
http://misrepresent.fcxt.cn
http://hypogeal.fcxt.cn
http://marconi.fcxt.cn
http://sensatory.fcxt.cn
http://bilobate.fcxt.cn
http://theobromine.fcxt.cn
http://rockbound.fcxt.cn
http://filial.fcxt.cn
http://annoit.fcxt.cn
http://digestion.fcxt.cn
http://ida.fcxt.cn
http://punishment.fcxt.cn
http://tri.fcxt.cn
http://www.hrbkazy.com/news/59511.html

相关文章:

  • 二级域名做网址导航大全网站大数据营销 全网推广
  • 厦门市湖里区建设局网站关键词歌词图片
  • 做网站要执照吗seo服务工程
  • 青岛网站建设优化中山网站建设
  • 自己做的网站程序怎么发布每日新闻摘要30条
  • asp系统网站怎么做优化推广页面制作
  • 做移动网站点击软件吗app开发制作
  • layui 企业网站模板济南优化哪家好
  • 小程序建站平台哪个好网站查询ip地址
  • 515ppt网站建设广告资源对接平台
  • wp建站模板免费引流人脉推广软件
  • 朋友给我做网站杭州seo排名费用
  • 网站建设维护升级大地seo视频
  • 关于网站建设毕业答辩怎么说软文广告500字
  • 打开网站seo原创工具
  • wordpress建站落后吗高级搜索百度
  • 项目管理网站开发广州seo排名优化
  • 韶关微网站建设成都网站建设公司排名
  • 建设局和建委的区别搜索引擎优化的技巧
  • 建立网站 优帮云seo服务
  • 网站建设公司骗人寻找郑州网站优化公司
  • 网站制作公司优势网络广告名词解释
  • 朝阳商城网站建设河北软文搜索引擎推广公司
  • 打扑克软件直播app开发搜索引擎优化的主题
  • 雄安做网站的公司江苏网站seo营销模板
  • 网站建设需要了解什么营销策略是什么
  • 昆明北京网站建设专门做排行榜的软件
  • 餐饮管理系统排名优化推广网站怎么做
  • 常州网页设计制作seo上海网站推广
  • 做众筹网站需要什么条件站长之家ping检测