Java多线程及线程安全实现方法解析

所属分类: 软件编程 / java 阅读数: 46
收藏 0 赞 0 分享

一、java多线程实现的两种方式

1、继承Thread

/**
 * 
 * @version: 1.1.0
 * @Description: 多线程
 * @author: wsq
 * @date: 2020年6月8日下午2:25:33
 */
public class MyThread extends Thread{
@Override
public void run() {
  System.out.println("This is the first thread!");
}
public static void main(String[] args) {
  MyThread mt = new MyThread();
  mt.start();
}
}

2、实现 Runnable 接口

public class MultithreadingTest {
public static void main(String[] args) {
  new Thread(() -> System.out.println("This is the first thread!")).start();
}
}

或者

public class MyThreadImpl implements Runnable{
private int count = 5;
  @Override
  public void run() {
    // TODO Auto-generated method stub
    count--;
    System.out.println("Thread"+Thread.currentThread().getName()+"count:"+count);
  }
}

二、解决线程不安全问题

/**
 * 
 * @version: 1.1.0
 * @Description: 测试类
 * @author: wsq
 * @date: 2020年6月8日下午9:27:02
 */
public class Test {
  public static void main(String[] args) {
    MyThreadImpl myThreadImpl = new MyThreadImpl();
    Thread A = new Thread(myThreadImpl,"A");
    Thread B = new Thread(myThreadImpl,"B");
    Thread C = new Thread(myThreadImpl,"C");
    Thread D = new Thread(myThreadImpl,"D");
    Thread E = new Thread(myThreadImpl,"E");
    A.start();
    B.start();
    C.start();
    D.start();
    E.start();
  }
}

打印结果为:

ThreadBcount:3
ThreadCcount:2
ThreadAcount:3
ThreadDcount:1
ThreadEcount:0

B和A共用一个线程,存在线程安全问题

改成:

public class MyThreadImpl implements Runnable{
private int count = 5;
  @Override
  // 使用同步解决线程安全问题
  synchronized public void run() {
    // TODO Auto-generated method stub
    count--;
    System.out.println("Thread"+Thread.currentThread().getName()+"count:"+count);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot SpEL语法扫盲与查询手册的实现

这篇文章主要介绍了SpringBoot SpEL语法扫盲与查询手册的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java创建子线程的两种方法

这篇文章主要介绍了Java创建子线程的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot2.x集成JPA快速开发的示例代码

这篇文章主要介绍了Spring Boot2.x集成JPA快速开发,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

关于Java中的mysql时区问题详解

这篇文章主要给大家介绍了关于Java中mysql时区问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

JAVA参数传递方式实例浅析【按值传递与引用传递区别】

这篇文章主要介绍了JAVA参数传递方式,结合实例形式分析了java按值传递与引用传递区别及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中MessageDigest来实现数据加密的方法

这篇文章主要介绍了Java中MessageDigest来实现数据加密的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

spring 注解验证@NotNull等使用方法

这篇文章主要介绍了spring 注解验证@NotNull等使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

浅谈如何优雅地停止Spring Boot应用

这篇文章主要介绍了浅谈如何优雅地停止Spring Boot应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Python如何使用@property @x.setter及@x.deleter

这篇文章主要介绍了Python如何使用@property @x.setter及@x.deleter,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java Jmeter全局变量设置过程图解

这篇文章主要介绍了Java Jmeter全局变量设置过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多