java中多线程 - 多线程中的基本方法

介绍一下线程中基本的方法使用

线程睡眠sleep()

Thread.sleep(毫秒);我们可以通过sleep方法设置让线程睡眠。可以看到sleep是个静态方法

public static native void sleep(long var0) throws InterruptedException;
    try {System.out.println(new Date().getSeconds());Thread.sleep(5000);System.out.println(new Date().getSeconds());} catch (InterruptedException e) {e.printStackTrace();}

setDaemon守护线程

非守护线程停止,那么守护线程自动退出

    public static void main(String[] args) {Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 5; i ++) {System.out.println("非守护线程");}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 200; i ++) {System.out.println("守护线程");}}};thread2.setDaemon(true);thread1.start();thread2.start();}

可以很明显的看到thread2本应该执行200次输出,但是这里只输出了几行。因为当thread1执行完毕后,thread2作为守护线程就自动停止了。
1445322-20180924200402581-294795725.png

多线程join

如果执行了join方法,那么停止当前线程,先跑执行了join()的线程。相当于插队执行。如下,在执行thread2线程的时候,如果i==20的时候,则让thread1插队先执行

    public static void main(String[] args) {final Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 500; i ++) {System.out.println("thread1---" + i);}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 200; i ++) {if (i == 20) {try {//插队执行thread1.join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(i);}}};thread1.start();thread2.start();}

join()方法也可以传参数long 毫秒 join(毫秒)
表示让执行join的线程,插队执行XXX毫秒,过了时间后,两个线程交替执行

   public static void main(String[] args) {final Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 500; i ++) {System.out.println("thread1---" + i);}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 200; i ++) {if (i == 20) {try {//插队执行1毫秒thread1.join(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(i);}}};thread1.start();thread2.start();}

yeild 礼让线程

yeild会让出cpu,让其他线程执行

    public static void main(String[] args) {final Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 500; i ++) {System.out.println( getName() + "---" + i);}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 200; i ++) {if (i % 5 == 0) {Thread.yield();}System.out.println(getName() + "---" + i);}}};thread1.start();thread2.start();}

setPriority给线程设置优先级

默认优先级是5 最小1,最大10
越大优先级越高

public static void main(String[] args) {final Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 500; i ++) {System.out.println( getName() + "---" + i);}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 500; i ++) {System.out.println(getName() + "---" + i);}}};//设置最大的线程优先级最大为10thread1.setPriority(Thread.MIN_PRIORITY);//设置最小的线程优先级,最小为1thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();}

synchronized

同步代码块

当多线程并发,多段代码同时执行的时候。希望在执行其中代码的时候,cpu不切换线程

不用synchronized的情况

我们来看一下不用synchronized的情况会发生什么

public class ThreadSynchronied {public static void main(String[] args) {final Say say = new Say();Thread thread1 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say();}}};Thread thread2 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say1();}}};//设置最大的线程优先级最大为10thread1.setPriority(Thread.MIN_PRIORITY);//设置最小的线程优先级,最小为1thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();}
}class Say {void say() {System.out.print("s ");System.out.print("a ");System.out.print("y ");System.out.print("h ");System.out.print("e ");System.out.print("l ");System.out.print("l ");System.out.println("o");}void say1() {System.out.print("1 ");System.out.print("2 ");System.out.print("3 ");System.out.print("4 ");System.out.print("5 ");System.out.print("6 ");System.out.print("7 ");System.out.println("8");}
}

1445322-20180924201221861-184513208.png

我们发现有些输出并没有打印全,在执行线程thread1的过程中,cpu被thread2抢占。这种情况下,肯定是不符合我们的业务逻辑的。所以我们要保证线程执行了一个完整的方法后,cpu才会被其他线程抢占

使用synchronized

public class ThreadSynchronied {public static void main(String[] args) {final Say say = new Say();Thread thread1 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say();}}};Thread thread2 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say1();}}};//设置最大的线程优先级最大为10thread1.setPriority(Thread.MIN_PRIORITY);//设置最小的线程优先级,最小为1thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();}
}class Say {String s = "hahaah";void say() {synchronized (s) {System.out.print("s ");System.out.print("a ");System.out.print("y ");System.out.print("h ");System.out.print("e ");System.out.print("l ");System.out.print("l ");System.out.println("o");}}void say1() {synchronized (s) {System.out.print("1 ");System.out.print("2 ");System.out.print("3 ");System.out.print("4 ");System.out.print("5 ");System.out.print("6 ");System.out.print("7 ");System.out.println("8");}}
}

1445322-20180924201313867-1916616418.png

使用synchronized同步代码块后,就发现不会出现上述情况了

同步方法

public class ThreadSynchroniedMethod {public static void main(String[] args) {final Say say = new Say();Thread thread1 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say();}}};Thread thread2 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say1();}}};//设置最大的线程优先级最大为10thread1.setPriority(Thread.MIN_PRIORITY);//设置最小的线程优先级,最小为1thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();}
}class Say {//在方法上加锁static synchronized void say() {System.out.print("s ");System.out.print("a ");System.out.print("y ");System.out.print("h ");System.out.print("e ");System.out.print("l ");System.out.print("l ");System.out.println("o");}static void say1() {synchronized (Say.class) {System.out.print("1 ");System.out.print("2 ");System.out.print("3 ");System.out.print("4 ");System.out.print("5 ");System.out.print("6 ");System.out.print("7 ");System.out.println("8");}}
}

同步方法指的就是在方法上加锁

静态同步方法的所对象是该类的字节码对象
非静态的同步方法锁对象是this

多个线程使用同一资源锁,容易造成死锁

什么是死锁?

死锁是指两个或两个以上的进程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。

线程安全类

Vector
StringBuffer
HashTable

线程不安全

ArrayList
StringBuilder
HashSet

java.util.Collections中有synchronizedList等方法,支持我们把线程不安全的集合转成线程安全的
1445322-20180924201344939-511625922.png

学习笔记

多次启动一个线程是非法的

转载于:https://www.cnblogs.com/amberbar/p/9696440.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/249437.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

nodejs匿名函数

https://www.cnblogs.com/sharpest/p/8056232.html

Deployment descriptor

Deployment descriptor 是指一种配置文件用于工件部署到一些container/engine。 在Java Platform&#xff0c;Enterprise Edition中&#xff0c;部署描述符描述了应如何部署组件&#xff0c;模块或应用程序&#xff08;如Web应用程序或企业应用程序&#xff09;。它指示部署工具…

cordova 一个将web应用程序封装成app的框架

cordova 一个将web应用程序封装成app的框架 cordova的详细介绍请参考这个链接&#xff1a;http://www.zhoujingen.cn/blog/7034.html 我接下来主要将如何搭建。 1.首先你需要下载几样东西 1.jdk. 2.android_SDK. 2.安装这两个&#xff0c;并配置环境变量 这里jdk的环境变量配置…

windows linux 子系统折腾记

最近买了部新电脑&#xff0c;海尔n4105的一体机&#xff0c;好像叫s7。 放在房间里面&#xff0c;看看资料。因为性能孱弱&#xff0c;所以不敢安装太强大的软件&#xff0c;然后又有一颗折腾的心。所以尝试了win10自带的linux子系统。然后在应用商店搜索linux推荐debian 系统…

nodejs闭包

一、什么是闭包&#xff1f; 官方”的解释是&#xff1a;闭包是一个拥有许多变量和绑定了这些变量的环境的表达式&#xff08;通常是一个函数&#xff09;&#xff0c;因而这些变量也是该表达式的一部分。 相信很少有人能直接看懂这句话&#xff0c;因为他描述的太学术。其实这…

《深入理解Java虚拟机》读书笔记八

第九章 类加载及执行子系统的案例与实战 Q&#xff1a;如果有10个WEB应用程序都是用Spring来进行组织管理的话&#xff0c;可以把Spring放到Common或Shared目录下&#xff08;Tomcat5.0&#xff09;让这些程序共享。Spring要对用户程序的类进行管理&#xff0c;自然要能访问到用…

一些非常有用的链接和工具

微信公众平台SDK Senparc.Weixin for C#&#xff0c;支持.NET Framework及.NET Core &#xff1a; https://github.com/JeffreySu/WeiXinMPSDK layui开发文档地址&#xff1a;https://www.layui.com/doc/ .Net Core GitHub社区 &#xff1a; https://github.com/dotnetcore EF…

Activity Intent相关FLAG介绍

先首先简单介绍下Task和Activity的关系 Task就像一个容器&#xff0c;而Activity就相当与填充这个容器的东西&#xff0c;第一个东西&#xff08;Activity&#xff09;则会处于最下面&#xff0c;最后添加的东西&#xff08;Activity&#xff09;则会在最上面。从Task中取出东西…

js的原型和原型链

构造函数创建对象&#xff1a; function Person() {} var person new Person(); person.name Kevin; console.log(person.name) // KevinPerson 就是一个构造函数&#xff0c;我们使用 new 创建了一个实例对象 person prototype 每个函数都有一个 prototype 属性 每一个Ja…

二维数组

要求&#xff1a;求一个二维数组的最大子数组和 思路&#xff1a;对于这个题&#xff0c;我会最简单的读取&#xff0c;虽然在网上查到了代码&#xff0c;但是查找最大子数组的循环我真的看不懂&#xff0c;也不是特别懂思路&#xff0c;所以在这不会写思路 package 二维数组; …

资源

资源链接&#xff1a; 内存池TinySTLminiSTLcghSTL1. lishuhuakai 2. 转载于:https://www.cnblogs.com/sunbines/p/9707483.html

Android判断应用或Activity是否存在

一、根据包名判断应用是否存在public boolean checkApplication(String packageName) { if (packageName null || "".equals(packageName)){ return false; } try { ApplicationInfo info getPackageManager().getApplicationInfo(packageName, PackageManager.GET…

vue ref

https://www.jianshu.com/p/623c8b009a85

033 Url中特殊字符的处理

在url跳转页面的时候&#xff0c;参数值中的#不见了&#xff0c;一直没有处理&#xff0c;今天有空看了一下&#xff0c;后来发现后台的过滤器之类的都没有处理&#xff0c;就比较奇怪了&#xff0c;原来是特殊字符的问题。 一&#xff1a;Url中的特殊字符 1.说明 这里还是需要…

Effective Java(1)-创建和销毁对象

Effective Java&#xff08;1&#xff09;-创建和销毁对象 转载于:https://www.cnblogs.com/Johar/p/10556218.html

什么是Affinity

什么是Affinity 在某些情况下&#xff0c;Android需要知道一个Activity属于哪个Task&#xff0c;即使它没有被启动到一个具体的Task里。这是通过任务共用性&#xff08;Affinities&#xff09;完成的。任务共用性&#xff08;Affinities&#xff09;为这个运行一个或多…

vue this

https://blog.csdn.net/cddcj/article/details/80866902

课外书——自控力(斯坦福大学最受欢迎的心理学课程)

01我要做&#xff0c;我不要&#xff0c;我想要&#xff1a;什么是意志力&#xff1f;为什么意志力&#xff1f;为什么意志力至关重要&#xff1f; 核心思想&#xff1a; 意志力实际上是“我要做”、“我不要”和“我想要”这三种力量。它们协同努力&#xff0c;让我们变成更好…

Docker运行GUI软件的方法

转自 https://www.csdn.net/article/2015-07-30/2825340 简介&#xff1a; Docker通过namespace将容器与主机上的网络和运行环境进行了隔离&#xff0c;默认情况下&#xff0c;在容器中运行带界面的软件在外部是看不到的。在这个分享中&#xff0c;将介绍通过共享X11套接字让外…

JS正则表达式验证数字非常全

验证数字的正则表达式集 验证数字&#xff1a;^[0-9]*$ 验证n位的数字&#xff1a;^\d{n}$ 验证至少n位数字&#xff1a;^\d{n,}$ 验证m-n位的数字&#xff1a;^\d{m,n}$ 验证零和非零开头的数字&#xff1a;^(0|[1-9][0-9]*)$ 验证有两位小数的正实数&#xff1a;^[0-9](.[0-9…