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

福安网站建设百度推广公司

福安网站建设,百度推广公司,学技巧网站制作,广东广州快速网站制作企业目录 一、Thread类基本用法 1.1 Thread常见构造方法 1.2 Thread常见属性 二、多线程常用的创建方式 2.1 继承Thread类 2.2 实现Runnable接口 2.3 继承Thread接口,使用匿名内部类 2.4实现Runnable接口,使用匿名内部类 2.5使用lambda表达式 三、线程的启动…

目录

一、Thread类基本用法

1.1 Thread常见构造方法

1.2 Thread常见属性

二、多线程常用的创建方式

2.1 继承Thread类

2.2 实现Runnable接口 

2.3 继承Thread接口,使用匿名内部类

2.4实现Runnable接口,使用匿名内部类

2.5使用lambda表达式

三、线程的启动

3.1 start()方法和run()方法的区别

四、线程终止

4.1通过共享标记来终止

4.2通过调用interrupt()方法来终止

五、多线程等待

六、多线程休眠

七、获取多线程实例对象


一、Thread类基本用法

1.1 Thread常见构造方法

方法说明
Thread()创建线程对象
Thread(Runnable target)使用 Runnable 对象创建线程对象
Thread(String name)创建线程对象,并命名
Thread(Runnable target, String name)使用 Runnable 对象创建线程对象,并命名
Thread(ThreadGroup group, Runnable target)线程可以被用来分组管理,分好的组即为线程组,了解即可

1.2 Thread常见属性

属性获取方法
IDgetId()
名称getName()
状态getState()
优先级getPriority()
是否后台线程isDaemon(),后台线程不影响进程的结束
是否存活isAlive()
是否被中断isInterrupted()

二、多线程常用的创建方式

2.1 继承Thread类

class MyThread extends Thread {//重写run方法@Overridepublic void run() {}
}public class test{public static void main(String[] args){MyThread t = new MyThread();t.start();}
}

2.2 实现Runnable接口 

class MyRunnable implements Runnable{//重写run方法@Overridepublic void run(){}
}public class test{public static void main(Strings[] args){MyRunnable runnable = new MyRunnable();//这个对象需要放在Thread中才能创建线程Thread t = new Thread(runnable);t.start();}
}

2.3 继承Thread接口,使用匿名内部类

public class test {public static void main(Strings[] args){Thread t = new Thread(){//重写run方法@Overridepublic void run(){}};t.start();}
}

2.4实现Runnable接口,使用匿名内部类

public class test{public static void main(Strings[] args){Thread t = new Thread(new Runnable{//重写run方法@Overridepublic void run(){}     });t.start();}
}

2.5使用lambda表达式

public class test{public static void main(String[] args){Thread t = new Thread(()->{//直接写具体的内容 },name);//可以自定义多线程的名字t.start();}
}

三、线程的启动

3.1 start()方法和run()方法的区别

  • start方法会调用api接口创建线程
  • run方法只是Thread类中的一个方法,不会创建出线程
  • start方法执行的过程中会并发执行其他任务
  • run方法不能并发执行其他任务,只能等run方法中的任务执行完才能执行下一个任务

四、线程终止

4.1通过共享标记来终止

这里会涉及到一个lambda表达式的变量捕获,当作为局部变量的时候最好是不可变的,作为成员变量的时候要是静态的。

public class Demo6 {//设置标记位public static boolean flag = false;public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{while (!flag) {System.out.println("hello Thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t.start();Thread.sleep(1000);flag = true;//通过将标志位转换达到停止程序的目的System.out.println("程序终止");}
}

4.2通过调用interrupt()方法来终止

调用interrupt()方法终止程序的时候,在唤醒程序后会将标志位进行清除,这里就会留有很大的操作空间供自己操作。包括是否立即终止、执行其他操作、继续执行当前代码。

方法说明
public void interrupt()中断对象关联的线程,如果线程正在阻塞,则以异常方式通知,
否则设置标志位
public static boolean
interrupted()
判断当前线程的中断标志位是否设置,调用后清除标志位
public boolean
isInterrupted()
判断对象关联的线程的标志位是否设置,调用后不清除标志位
public class Demo7 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{、//利用isInterrupted来设置标记while (!Thread.currentThread().isInterrupted()) {System.out.println("hello Thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();break;}}});t.start();Thread.sleep(3000);t.interrupt();//将标记转换进而终止程序System.out.println("程序终止");}
}

五、多线程等待

多线程的执行是随机的,但是一般程序的编写我们都是希望有顺序的执行和结束,所以这里采用join()方法对线程进行等待。当一个线程加入join()方法时,就要阻塞等待调用该方法的线程执行完才能执行结束当前线程。

方法说明
public void join()等待线程结束
public void join(long millis)等待线程结束,最多等 millis 毫秒
public void join(long millis, int nanos)同理,但可以更高精度
public class Demo8 {public static void main(String[] args) {Thread t1 = new Thread(()->{for (int i = 0; i < 3; i++) {System.out.println("t1正在执行");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("t1结束");});Thread t2 = new Thread(()->{for (int i = 0; i < 3; i++) {System.out.println("t2正在执行");try {Thread.sleep(1000);t1.join();//需要等待t1执行完毕} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("t2结束");});t1.start();t2.start();}
}

六、多线程休眠

也是我们比较熟悉一组方法,有一点要记得,因为线程的调度是不可控的,所以,这个方法只能保证实际休眠时间是大于等于参数设置的休眠时间的

方法说明
public static void sleep(long millis) throws InterruptedException休眠当前线程 millis
毫秒
public static void sleep(long millis, int nanos) throws InterruptedException可以更高精度的休眠
public class ThreadDemo {public static void main(String[] args) throws InterruptedException {System.out.println("开始");Thread.sleep(1000);//线程进行休眠1sSystem.out.println("结束");}
}

七、获取多线程实例对象

方法说明
public static Thread currentThread();返回当前线程对象的引用
public class ThreadDemo {public static void main(String[] args) {Thread thread = Thread.currentThread();System.out.println(thread.getName());}
}
http://www.hkea.cn/news/570835/

相关文章:

  • 腾讯云服务器网站建设淘宝推广哪种方式最好
  • 大专网站建设论文找个免费的网站
  • 移动端网站开发流程图seopeix
  • 购物网站制作免费太原seo招聘
  • 怎么建设食品网站济南seo外包公司
  • 建设网站有哪些seopeix
  • 桂林市工程建设项目招标网站莆田百度快照优化
  • 金华网站建设大型网页建设农产品网络营销
  • wordpress free cdn长沙百度快速优化
  • 网页界面设计首页seo快速优化软件网站
  • 和凡科网类似的网站四川省人民政府
  • 北辰网站建设如何推广引流
  • ps网页模板网站seo外包公司
  • 常平镇仿做网站快速排名刷
  • 青浦建设网站公司app推广代理加盟
  • wordpress 在线pdf优化关键词的正确方法
  • 网站悬浮窗口网站关键词全国各地的排名情况
  • 做网站得叫什么优化关键词排名
  • 丰县住房与城乡建设部网站太原网站制作优化seo公司
  • 微信如何做微商城网站建设手机网站智能建站
  • 网站尾部分页数字怎么做推广app大全
  • 建筑设计软件有哪些优化网站建设
  • 网站开发 word文件预览医疗器械龙头股
  • 电子商务网站建设花费南宁百度seo排名价格
  • 做公司网站要注意哪些问题真正免费建站网站
  • 在线服务器代理杭州seo网络公司
  • wordpress邮件订阅seo技术外包
  • 深圳营销网站建站公司搜索引擎关键词的工具
  • 做网站如何网站考虑优化游戏推广员是诈骗吗
  • 公众号做视频网站吗关键词排名怎么做上首页