Java多线程之syncrhoized内置互斥锁的用法详解
import java.lang.Thread.State; import org.omg.CORBA.PUBLIC_MEMBER; public class ThreadTest {
public static void main(String[] args) throws Exception{
MyRunnable m1=new MyRunnable();
Thread t1=new Thread(m1);
Thread t2=new Thread(m1);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(MyRunnable.num);
}
}
class MyRunnable implements Runnable
{
public static int num=0;
public synchronized void add()
{
for(int i=0;i<100;++i)
{
num++;
}
}
@Override
public void run() {
add();
}
}
结果:200
import java.lang.Thread.State; import org.omg.CORBA.PUBLIC_MEMBER; public class ThreadTest {
public static void main(String[] args) throws Exception{
MyRunnable m1=new MyRunnable();
MyRunnable m2=new MyRunnable();
Thread t1=new Thread(m1);
Thread t2=new Thread(m2);
t1.start();
t2.start(); t1.join();
t2.join();
System.out.println(MyRunnable.num);
}
}
class MyRunnable implements Runnable
{
public static int num=0;
public synchronized static void add()
{
for(int i=0;i<100;++i)
{
num++;
}
}
@Override
public void run() {
add();
}
}
结果:200
public Object lock=new Object();
}
import java.lang.Thread.State; import org.omg.CORBA.PUBLIC_MEMBER; public class ThreadTest {
public static void main(String[] args) throws Exception{
MyRunnable m1=new MyRunnable(); Thread t1=new Thread(m1);
Thread t2=new Thread(m1);
t1.start();
t2.start(); t1.join();
t2.join();
System.out.println(MyRunnable.num);
}
}
class MyRunnable implements Runnable
{
public static int num=0;
public Object lock=new Object();
public void add()
{
synchronized (lock) {
for(int i=0;i<100000;++i)
{
num++;
}
}
}
@Override
public void run() {
add();
}
}
结果:200000
import java.lang.Thread.State; import org.omg.CORBA.PUBLIC_MEMBER; public class ThreadTest {
public static void main(String[] args) throws Exception{
MyRunnable m1=new MyRunnable();
MyRunnable m2=new MyRunnable();
Thread t1=new Thread(m1);
Thread t2=new Thread(m2);
t1.start();
t2.start(); t1.join();
t2.join();
System.out.println(MyRunnable.num);
}
}
class MyRunnable implements Runnable
{
public static int num=0;
public Object lock=new Object();
public void add()
{
synchronized (lock) {
for(int i=0;i<100000;++i)
{
num++;
}
}
}
@Override
public void run() {
add();
}
}
多调试几次:结果是随机的,循环次数越大结果越随机。比如,上面for循环中如果是i<100,那么可能结果总是200,这看起来是对的,没错。原因是循环次数太少,两线程对结果影响不大。当把数字调大,如100000时,结果就很随机了,有可能是103453,112378。。。。。。。
Java多线程之syncrhoized内置互斥锁的用法详解的更多相关文章
- “全栈2019”Java多线程第三十章:尝试获取锁tryLock()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- Angular2 内置指令 NgFor 和 NgIf 详解
http://www.jb51.net/article/89781.htm 在这一章节中,我们来学习如何使用Angular2来展示数据,以及如何使用它的内置指令NgFor和NgIf 首先要确保你有一个 ...
- PHP使用内置函数生成图片的方法详解
原文地址:http://www.poluoluo.com/jzxy/201605/475301.html 本文实例讲述了PHP使用内置函数生成图片的方法.分享给大家供大家参考,具体如下: 第一步:创建 ...
- Win7下的内置FTP组件的设置详解
在局域网中共享文件,FTP是比较方便的方案之一.Win7内部集成了FTP,只是设置起来颇费一番功夫.着文以记之. 一.安装FTP组件 由于Win7默认没有安装FTP组件.故FTP的设置第一步就是安装F ...
- [Form Builder]内置函数execute_trigger、do_key详解
转:http://yedward.net/?id=82 1.execute_trigger:用来运行一个指定的触发器,常用来运行用户自定义的触发器. 语法:procedure execute_trig ...
- java中的Static、final、Static final各种用法详解
前言 对Static.final.Static final这几个关键词熟悉又陌生?想说却又不知怎么准确说出口?好的,本篇博客文章将简短概要出他们之间的各自的使用,希望各位要是被你的面试官问到了,也能从 ...
- Java多线程之Runnable与Thread
Java多线程之Thread与Runnable 一.Thread VS Runnable 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类和 ...
- Java多线程之ConcurrentSkipListMap深入分析(转)
Java多线程之ConcurrentSkipListMap深入分析 一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...
- JAVA多线程之wait/notify
本文主要学习JAVA多线程中的 wait()方法 与 notify()/notifyAll()方法的用法. ①wait() 与 notify/notifyAll 方法必须在同步代码块中使用 ②wait ...
随机推荐
- EditText的监听器和自定义回车事件
我们一般是监听EditText的状态,看EditText中是不是有文字,根据有无进行不同的操作. // 给editText添加监听器 editText.addTextChangedListener(n ...
- 深入理解多线程(三)—— Java的对象头
上一篇文章中我们从HotSpot的源码入手,介绍了Java的对象模型.这一篇文章在上一篇文章的基础上再来介绍一下Java的对象头.主要介绍一下对象头的作用,结构以及他和锁的关系. Java对象模型回顾 ...
- 对Kalman(卡尔曼)滤波器的理解@@zz
1.简介(Brief Introduction) 在学习卡尔曼滤波器之前,首先看看为什么叫“卡尔曼”.跟其他著名的理论(例如傅立叶变换,泰勒级数等等)一样,卡尔曼也是一个人的名字,而跟他们不同的是,他 ...
- windows 通过Web.config添加mimetype映射
在Web.config里添加以下代码即可 <configuration> <system.webServer> <staticContent> <!-- re ...
- linux命令大全网站
一. linux命令大全网站 http://man.linuxde.net/watch
- DaoCloud加速docker镜像下载
1. 注册DaoCloud用户; 2. 注册完成后,会进入dashboard页面,点击右上方的加速器.该页面提供了Linux.Windows和Mac的加速方案,我这里选择的是Linux: 3. 执行其 ...
- (转)Unity3D研究院之Assetbundle的实战(六十三)
上一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity3D研究院之Assetbundle的原理(六十一) 本篇文章我们将说说assetbundl ...
- CMenu and Dialog-based applications
[问] Is it possible to put a menu in a dialog based application? How? [答] Yes, it is possible to add ...
- Express application generator的使用
首先拷贝express官网的一篇文章: (http://expressjs.com/en/starter/generator.html ) Express application generator ...
- rabbitmq页面出现/etc/rabbitmq/rabbitmq.config(not found)解决方法
如果出现页面出现/etc/rabbitmq/rabbitmq.config(not found) 解决如下:find / -name "rabbitmq.config.example&quo ...