package com.multi;public class ThreadTest implements Runnable {
private Integer ticket = 10;
private Integer count = 0;
private byte[] lock = new byte[0]; public void run() {
synchronized (ticket) {
while (true) {
if (ticket <= -100) {
break;
}
System.out.println(String.format("thread: %s , tiketnum = %d do some thing"
,Thread.currentThread().getName()
,ticket--));
count++;
}
}
}
public static void main(String[] args) {
ThreadTest mTh1 = new ThreadTest();
Thread[] th = new Thread[5];
for (int i = 0; i < 5; i++) {
th[i]= new Thread(mTh1,"th"+i);
th[i].start();
}
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(mTh1.count);
}
}执行结果
thread: th4 , tiketnum = 6 do some thing
thread: th3 , tiketnum = 7 do some thing
thread: th4 , tiketnum = 5 do some thing
thread: th3 , tiketnum = 4 do some thing
thread: th3 , tiketnum = 2 do some thing
thread: th4 , tiketnum = 3 do some thing
thread: th4 , tiketnum = 0 do some thing
thread: th4 , tiketnum = -1 do some thing
thread: th4 , tiketnum = -2 do some thing
thread: th4 , tiketnum = -3 do some thing
thread: th4 , tiketnum = -4 do some thing
thread: th4 , tiketnum = -5 do some thing
thread: th4 , tiketnum = -6 do some thing
thread: th4 , tiketnum = -7 do some thing
thread: th4 , tiketnum = -8 do some thing
thread: th1 , tiketnum = 9 do some thing
………………………………
private Integer ticket = 10;
private Integer count = 0;
private byte[] lock = new byte[0]; public void run() {
synchronized (ticket) {
while (true) {
if (ticket <= -100) {
break;
}
System.out.println(String.format("thread: %s , tiketnum = %d do some thing"
,Thread.currentThread().getName()
,ticket--));
count++;
}
}
}
public static void main(String[] args) {
ThreadTest mTh1 = new ThreadTest();
Thread[] th = new Thread[5];
for (int i = 0; i < 5; i++) {
th[i]= new Thread(mTh1,"th"+i);
th[i].start();
}
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(mTh1.count);
}
}执行结果
thread: th4 , tiketnum = 6 do some thing
thread: th3 , tiketnum = 7 do some thing
thread: th4 , tiketnum = 5 do some thing
thread: th3 , tiketnum = 4 do some thing
thread: th3 , tiketnum = 2 do some thing
thread: th4 , tiketnum = 3 do some thing
thread: th4 , tiketnum = 0 do some thing
thread: th4 , tiketnum = -1 do some thing
thread: th4 , tiketnum = -2 do some thing
thread: th4 , tiketnum = -3 do some thing
thread: th4 , tiketnum = -4 do some thing
thread: th4 , tiketnum = -5 do some thing
thread: th4 , tiketnum = -6 do some thing
thread: th4 , tiketnum = -7 do some thing
thread: th4 , tiketnum = -8 do some thing
thread: th1 , tiketnum = 9 do some thing
………………………………
System.out.println(String.format("thread: %s , tiketnum = %d do some thing",Thread.currentThread().getName(),ticket--));如果将ticket--移出来,改写成
System.out.println(String.format("thread: %s , tiketnum = %d do some thing",Thread.currentThread().getName(),ticket));
ticket--;
输出就正常了
thread: th0 , tiketnum = 10 do some thing
thread: th0 , tiketnum = 9 do some thing
thread: th0 , tiketnum = 8 do some thing
thread: th0 , tiketnum = 7 do some thing
thread: th0 , tiketnum = 6 do some thing
thread: th0 , tiketnum = 5 do some thing
thread: th0 , tiketnum = 4 do some thing
thread: th0 , tiketnum = 3 do some thing
thread: th0 , tiketnum = 2 do some thing
thread: th0 , tiketnum = 1 do some thing
thread: th0 , tiketnum = 0 do some thing
thread: th0 , tiketnum = -1 do some thing
……………………
………………
第二种写法,先打印,这样就有可能给启动第二个线程机会了。
不信,你可以改改--操作的位置
这是我觉得唯一可能的解释了。如果是一般的对像的话,是肯定可以锁住的。还真是,将--操作移到println前面,输出又是乱序执行了。不过你说的一般对象和integer不是纯粹的对象指的是?
一个对象一把锁。
基础类型对象是存放在Java虚拟机栈中的,你每次操作改变ticket的时候相当于重新在栈中创建了对象,你拿到的锁是当前对象的锁,所以说你申请的不是同一个对象的锁。而引用对象会在栈里面存放一个引用,实际对象存放在堆空间里面,变量名相当于一个引用,你引用不变那么指向的就是同一份堆内存空间,也就是同一个对象,所以synchronized的时候不能是简单类型的对象。
说话不严谨,是单线程执行了呢
拿到哪里?外面指的是哪行?
拿到哪里?外面指的是哪行?
看八楼
对于一般性锁的解释,21楼是正确的。但对于你这段代码,相反的,我不认为21楼的解释完全正确。其实最关键的在于ticket-- 这一句话的理解简单来说,ticket--这句话,已经让ticket这个变量指向了一个新的Integer对像,所以你才会锁不住。但今天特地去查了一下,其实答案非常非常的有意思这个和integer对像的autoboxing 和unboxing这个功能有关系https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html简单来说,ticket-- 这句话,等同于 ticket = new Integer(ticket.intValue-1)。而不是什么ticket.decrease()这样的一个函数。所以,如果你只想搞清楚对像锁是怎么回事,那么就用一般的object对象,而不是integer或float之类有autoboxing 和unboxing功能的对象。
对于一般性锁的解释,21楼是正确的。但对于你这段代码,相反的,我不认为21楼的解释完全正确。其实最关键的在于ticket-- 这一句话的理解简单来说,ticket--这句话,已经让ticket这个变量指向了一个新的Integer对像,所以你才会锁不住。但今天特地去查了一下,其实答案非常非常的有意思这个和integer对像的autoboxing 和unboxing这个功能有关系https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html简单来说,ticket-- 这句话,等同于 ticket = new Integer(ticket.intValue-1)。而不是什么ticket.decrease()这样的一个函数。所以,如果你只想搞清楚对像锁是怎么回事,那么就用一般的object对象,而不是integer或float之类有autoboxing 和unboxing功能的对象。这样来看,包装类对象从设计上就不合适用作锁。
有一点需要更正下,如果同步语句块中代码是
System.out.println(String.format("thread: %s , tiketnum = %d do some thing",Thread.currentThread().getName(),ticket));
ticket--;
那么执行结果与线程数目有关,当线程数量较少时,数十个或者更少,那么无论测试多少遍,执行结果一直是单线程的;
当线程数目上升到数百个左右时,执行结果就是乱序执行了。
出现这种现象的原因应该是什么,资源竞争激烈影响了底层的线程调度?
取决于你后面线程执行到 synchronized (ticket)时,别的线程有没有执行过 ticket--;这一句。一旦有一个线程执行到了ticket--,那么这之后执行到synchronized 都锁不住。