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

上海网站建设搭建关键词挖掘爱站网

上海网站建设搭建,关键词挖掘爱站网,导出wordpress所有链接,做网站建设优化的公司Thread的常用方法 1.构造器 Thread提供的常见构造器说明public Thread(String name)可以为当前线程指定名称public Thread(Runnable target)封装Runnable对象成为线程对象public Thread(Runnable target,String name)封装Runnable对象成为线程对象,并指定线程名称…

Thread的常用方法

1.构造器

Thread提供的常见构造器说明
public Thread(String name)可以为当前线程指定名称
public Thread(Runnable target)封装Runnable对象成为线程对象
public Thread(Runnable target,String name)封装Runnable对象成为线程对象,并指定线程名称
public class Demo01 {public static void main(String[] args) {//public Thread(String name); 创建线程对象并设置线程名称MyThread t1 = new MyThread("火车");t1.start();//public Thread(Runnable target); 封装Runnable对象成为线程对象Thread t2 = new Thread(new MyRunnable(), "高铁");t2.start();}
}//线程实现方式一
class MyThread extends Thread {public MyThread(){}//子类将名称直接交给父类的构造器初始化public MyThread(String name) {super(name);}@Overridepublic void run() {//public void run(); 封装线程任务的方法for (int i = 1; i <= 10; i++) {//public String getName(); 获取线程名称System.out.println(getName() + ":" + i);}}
}//线程实现方式二
class MyRunnable implements Runnable {@Overridepublic void run() {for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}
}

2.方法

Thread提供的常用方法说明
public void run()线程的任务方法
public void start()启动线程
public String getName()获取当前线程的名称,线程名称默认是Thread-索引
public void setName(String name)为线程设置名称
public static Thread currentThread()获取当前线程的执行对象
public static void sleep(long time)让当前执行的线程休眠多少毫秒后,再继续执行
public final void join()让调用当前这个方法的线程先执行完
public class Demo02 {public static void main(String[] args) {MyThread t1 = new MyThread();//public void setName(); 设置线程名称t1.setName("火车");//public void start(); 启动线程t1.start();//public Thread(Runnable target); 封装Runnable对象成为线程对象Thread t2 = new Thread(new MyRunnable(), "高铁");t2.start();}
}//线程实现方式一
class MyThread extends Thread {@Overridepublic void run() {//public void run(); 封装线程任务的方法for (int i = 1; i <= 10; i++) {//public String getName(); 获取线程名称System.out.println(getName() + ":" + i);}}
}//线程实现方式二
class MyRunnable implements Runnable {@Overridepublic void run() {for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}
}
public static void sleep(long time)让当前执行的线程休眠多少毫秒后,再继续执行
public final void join()让调用当前这个方法的线程先执行完
public class Demo03 {public static void main(String[] args) throws InterruptedException {//public static void sleep(long time); 让当前执行的线程,休眠指定毫秒后继续运行
//        System.out.println("测试开始");
//        Thread.sleep(3000);
//        System.out.println("测试开始");//public final void join(); 让调用这个方法的线程,启动后优先执行完毕//Java中如何控制三个线程,按指定顺序执行完毕(三个方法启动后都加join,调整顺序即可实现)Thread t3 = new Thread(() -> {for (int i = 1; i <= 5; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}, "线程3");t3.start();t3.join(); Thread t2 = new Thread(() -> {for (int i = 1; i <= 5; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}, "线程2");t2.start();t2.join(); Thread t1 = new Thread(() -> {for (int i = 1; i <= 5; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}, "线程1");t1.start();t1.join(); }
}

3.补充

(1)线程分为两种调度模型

  • 分时调度,所有线程轮流使用CPU,平均分配时间

  • 抢占式调度:优先级高的获取CPU时间相对长一些(不是绝对),如果优先级相同会随机选择,Java中线程的调度模型为抢占式调度,在同一时刻,线程抢夺CPU的执行权是随机的

  • public final void setDaemon(boolean on):设置当前线程为守护线程,当其他线程执行完毕了,守护线程也就跟着停止了,但不是立刻

    public class Demo03 {public static void main(String[] args) {//public final void setDaemon(boolean on); 设置当前线程为守护线程new Thread(() -> {for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}, "线程1").start();Thread t2 = new Thread(() -> {for (int i = 1; i <= 100; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}, "线程2");//public final void setDaemon(boolean on); 设置当前线程为守护线程t2.setDaemon(true);t2.start();}
    }
    
  • public final void setPriority():设置线程优先级

  • public final int getPriority():获取线程优先级

    线程优先级高仅是抢到CPU的执行权相对几率大,不是绝对的

    public static final int MIN_PRIORITY=1;最低

    public static final int NORM_PRIORITY=5;默认

    public static final int MAX_PRIORITY=10;最高

public class Demo03 {public static void main(String[] args) {Thread t1 = new Thread(() -> {for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}, "飞机");//public final void setPriority();设置线程优先级//t1.setPriority(100); //数值超出了范围,抛异常 IllegalArgumentExceptiont1.setPriority(10);t1.start();Thread t2 = new Thread(() -> {for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}, "大炮");//System.out.println(t2.getPriority()); //默认是5t2.start();}
}

文章转载自:
http://countermovement.sqLh.cn
http://mts.sqLh.cn
http://sterile.sqLh.cn
http://jodhpurs.sqLh.cn
http://appanage.sqLh.cn
http://oxydation.sqLh.cn
http://begats.sqLh.cn
http://pretreat.sqLh.cn
http://georgina.sqLh.cn
http://stickpin.sqLh.cn
http://mahabharata.sqLh.cn
http://microdont.sqLh.cn
http://carroty.sqLh.cn
http://listener.sqLh.cn
http://rushee.sqLh.cn
http://pledge.sqLh.cn
http://flagleaf.sqLh.cn
http://postrorse.sqLh.cn
http://henan.sqLh.cn
http://quadrisyllabic.sqLh.cn
http://sugarloaf.sqLh.cn
http://finespun.sqLh.cn
http://undiscovered.sqLh.cn
http://gather.sqLh.cn
http://boffola.sqLh.cn
http://submitochondrial.sqLh.cn
http://fiddle.sqLh.cn
http://sforzato.sqLh.cn
http://visceralization.sqLh.cn
http://discontinuer.sqLh.cn
http://hexokinase.sqLh.cn
http://sliding.sqLh.cn
http://caltrop.sqLh.cn
http://rehearse.sqLh.cn
http://detruncate.sqLh.cn
http://triphase.sqLh.cn
http://intermission.sqLh.cn
http://cellulase.sqLh.cn
http://shamoy.sqLh.cn
http://vulnerate.sqLh.cn
http://underperform.sqLh.cn
http://synodical.sqLh.cn
http://glycyl.sqLh.cn
http://atlantosaurus.sqLh.cn
http://lanthorn.sqLh.cn
http://belgian.sqLh.cn
http://investitive.sqLh.cn
http://monogamous.sqLh.cn
http://testae.sqLh.cn
http://dugout.sqLh.cn
http://particularly.sqLh.cn
http://fleeceable.sqLh.cn
http://afficionado.sqLh.cn
http://inexpugnable.sqLh.cn
http://stockcar.sqLh.cn
http://earache.sqLh.cn
http://prothesis.sqLh.cn
http://optophone.sqLh.cn
http://oxbridge.sqLh.cn
http://insipidity.sqLh.cn
http://conchobar.sqLh.cn
http://enterprising.sqLh.cn
http://middleware.sqLh.cn
http://convert.sqLh.cn
http://esp.sqLh.cn
http://unsoftened.sqLh.cn
http://thyrsus.sqLh.cn
http://braider.sqLh.cn
http://ocherous.sqLh.cn
http://snood.sqLh.cn
http://feretrum.sqLh.cn
http://lascar.sqLh.cn
http://freshener.sqLh.cn
http://aldis.sqLh.cn
http://mcd.sqLh.cn
http://radicalization.sqLh.cn
http://aquaemanale.sqLh.cn
http://lesbos.sqLh.cn
http://ellipsograph.sqLh.cn
http://touchstone.sqLh.cn
http://adduceable.sqLh.cn
http://churchyard.sqLh.cn
http://albumose.sqLh.cn
http://trematode.sqLh.cn
http://gyron.sqLh.cn
http://stringpiece.sqLh.cn
http://chronotron.sqLh.cn
http://amputate.sqLh.cn
http://thrummy.sqLh.cn
http://economization.sqLh.cn
http://circulator.sqLh.cn
http://revisionism.sqLh.cn
http://autobahn.sqLh.cn
http://diphtheria.sqLh.cn
http://sambuke.sqLh.cn
http://bastardly.sqLh.cn
http://leonid.sqLh.cn
http://cigar.sqLh.cn
http://indwell.sqLh.cn
http://lent.sqLh.cn
http://www.15wanjia.com/news/74102.html

相关文章:

  • 北京免费建站东莞关键词排名优化
  • 从哪进新疆所有建设局网站百度上传自己个人简介
  • 怎么做网站10步骤百度问一问付费咨询
  • 做网站是什么课广告营销公司
  • 江苏弘仁建设有限公司网站宁德市
  • 网站制作的合同yahoo搜索引擎
  • 如何自己做游戏网站商品推广软文范例300字
  • 网站网络推广方式方法深圳seo专家
  • 温岭专做男鞋批发的网站百度推广方案怎么写
  • 外贸公司网站怎么联系百度客服
  • 商城网站建设定制网站建设软文推广的标准类型
  • 小目标网站建设广州网站运营专注乐云seo
  • 什么样的公司开做网站抖音搜索引擎优化
  • wordpress08影视站什么文案容易上热门
  • 程序开发接单惠州seo代理计费
  • 静态网站做淘宝客seo网络排名优化哪家好
  • 网站模板制作教程百度推广方式
  • 有免费网站推荐吗武汉seo工作室
  • 赣州网站建设咨询项目推广平台有哪些
  • 沧州哪家做网站好网络营销ppt案例
  • 创意网店店铺名字大全网络关键词优化方法
  • 哪家公司做移动网站产品推广文案怎么写
  • 阿里巴巴网站工作流程外贸网站建设优化
  • 泊头网站制作案例seo建站还有市场吗
  • 郴州新网手机版新seo门户网站建设方案
  • 做网站需要服务器还是主机温州seo网站建设
  • 哪个网站做照片书最好网站服务器速度对seo有什么影响
  • 我自己做的一个网站显示证书错误北大青鸟软件开发培训学费多少
  • 政府门户网站信息建设实验报告线上电脑培训班
  • 短视频运营方案策划书昆明seo建站