多线程
小小雨伞
这个作者很懒,什么都没留下…
展开
-
Python之线程休眠
import time# 停止10stime.sleep(10)# 或者from time import sleep# 停止1ssleep(1)原创 2020-10-07 23:20:59 · 2399 阅读 · 0 评论 -
javaSE之多线程之守护线程上帝和你
package state;//测试守护线程public class TestDaemon { public static void main(String[] args) { God god =new God(); You you = new You(); Thread thread = new Thread(god); thread.setDaemon(true);//默认为false //守护线程开始原创 2020-10-07 23:03:55 · 100 阅读 · 0 评论 -
javaSE之多线程之优先级
package state;public class TestPriority { //主线程默认优先级 public static void main(String[] args) { System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); MyPriority priority = new MyPrior原创 2020-10-07 23:01:02 · 95 阅读 · 0 评论 -
javaSE之多线程之礼让线程
礼让不一定成功,看CPU心情package state;//测试礼让线程public class Yield { public static void main(String[] args) { MyYield myYield =new MyYield(); new Thread(myYield,"A").start(); new Thread(myYield,"B").start(); }}class MyYi原创 2020-10-07 22:59:26 · 111 阅读 · 0 评论 -
javaSE之多线程之使用标志位控制线程停止
package state;public class TestStop implements Runnable { //设置一个标志位 private boolean flag = true; @Override public void run() { int i=0; while (flag){ System.out.println("run.....Thread"+i++); }原创 2020-10-07 22:57:08 · 199 阅读 · 0 评论 -
javaSE之多线程使用sleep模拟倒计时
//模拟倒计时 : 放大问题的反生性public class TestSleep { public static void main(String[] args) { //打印系统当前时间 Date startDate =new Date(System.currentTimeMillis()); while (true){ try { Thread.sleep(1000);原创 2020-10-07 22:55:44 · 303 阅读 · 0 评论 -
多线程之模拟龟兔赛跑
//模拟龟兔赛跑public class Race implements Runnable { //胜利者 private static String winner; @Override public void run() { for (int i=0;i<=100;i++){ //模拟兔子休息 try { if (Thread.currentThread().ge原创 2020-09-12 21:04:30 · 290 阅读 · 0 评论 -
多线程之线程不安全
//多个线程同时操作同一个对象//问题:多个线程操作同一个资源的情况下,线程不安全了public class TestThread2 implements Runnable { //票数 private int ticksNums = 10; @Override public void run() { while (true){ if (ticksNums<=0){ break;原创 2020-09-12 21:00:10 · 86 阅读 · 0 评论 -
多线程之创建多线程方法2:实现Runnable
//创建线程方式2//实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类public class TestThread1 implements Runnable { @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println("我在学习代码" + i); } } //主线程原创 2020-09-12 20:57:30 · 129 阅读 · 0 评论 -
多线程之创建多线程方法一:Theard
//创建线程方式一:继承thread类,重写run方法,调用startpublic class TestThread extends Thread { @Override public void run() { for (int i = 0; i <20 ; i++) { System.out.println("我在学习代码"+i); } } //主线程 public static void mai原创 2020-09-11 17:17:11 · 450 阅读 · 0 评论