Java线程的同步 - synchronized
2014-09-17 15:51
316 查看
为什么要在线程里面使用同步 - synchronized
首先看个列子:
假设系统里面有5张票,有个卖票的系统,执行完,打印的结果是这样的:
public class RunDemo05 implements Runnable {
private int count = 5;
public void run()
{
for(int i=0;i<10;i++)
{
sale();
}
}
public void sale(){
if (count>0)
{
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(count--);
}
}
public static void main(String[] args) {
RunDemo05 r = new RunDemo05();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
}
}
结果:
5
3
4
2
1
2
出现这样的原因: 在同一时刻有多个线程访问count数据,所以导致这种结果。
synchronized可以解决这个问题,它是的系统里面的某一时刻只有一个线程在运行。
synchronized的使用方法: 1. 使用synchronized方法 2. 使用synchronized程序块
package com.pwc.thread;
public class RunDemo05 implements Runnable {
private int count = 5;
public void run()
{
for(int i=0;i<10;i++)
{
sale();
}
}
public synchronized void sale(){
if (count>0)
{
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(count--);
}
}
public static void main(String[] args) {
RunDemo05 r = new RunDemo05();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
}
}
结果:
5
4
3
2
1
使用synchronized程序块:
package com.pwc.thread;
public class RunDemo05 implements Runnable {
private int count = 5;
public void run()
{
for(int i=0;i<10;i++)
{
sale();
}
}
public void sale(){
synchronized(this){
if (count>0)
{
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(count--);
}
}
}
public static void main(String[] args) {
RunDemo05 r = new RunDemo05();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
}
}
结果:
5
4
3
2
1
首先看个列子:
假设系统里面有5张票,有个卖票的系统,执行完,打印的结果是这样的:
public class RunDemo05 implements Runnable {
private int count = 5;
public void run()
{
for(int i=0;i<10;i++)
{
sale();
}
}
public void sale(){
if (count>0)
{
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(count--);
}
}
public static void main(String[] args) {
RunDemo05 r = new RunDemo05();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
}
}
结果:
5
3
4
2
1
2
出现这样的原因: 在同一时刻有多个线程访问count数据,所以导致这种结果。
synchronized可以解决这个问题,它是的系统里面的某一时刻只有一个线程在运行。
synchronized的使用方法: 1. 使用synchronized方法 2. 使用synchronized程序块
package com.pwc.thread;
public class RunDemo05 implements Runnable {
private int count = 5;
public void run()
{
for(int i=0;i<10;i++)
{
sale();
}
}
public synchronized void sale(){
if (count>0)
{
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(count--);
}
}
public static void main(String[] args) {
RunDemo05 r = new RunDemo05();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
}
}
结果:
5
4
3
2
1
使用synchronized程序块:
package com.pwc.thread;
public class RunDemo05 implements Runnable {
private int count = 5;
public void run()
{
for(int i=0;i<10;i++)
{
sale();
}
}
public void sale(){
synchronized(this){
if (count>0)
{
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(count--);
}
}
}
public static void main(String[] args) {
RunDemo05 r = new RunDemo05();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
}
}
结果:
5
4
3
2
1
相关文章推荐
- Java线程同步机制synchronized关键字的理解
- Java synchronized同步线程机制(三)
- Java线程同步:synchronized锁住的是代码还是对象
- Java线程及同步(synchronized)样例代码 [转]
- Java线程同步机制synchronized关键字的理解
- Java线程同步中关键字synchronized详述
- Java线程同步:synchronized锁住的是代码还是对象
- Java线程同步synchronized和volatile
- Java基础巩固之线程的同步synchronized
- Java synchronized同步线程机制(二)
- Java线程同步(synchronized wait notify)
- Java线程同步synchronized
- Java线程及同步(synchronized)样例代码
- Java线程同步:synchronized锁住的是代码还是对象
- java线程同步之synchronized
- Java synchronized同步线程机制(一)
- Java线程同步:synchronized锁住的是代码还是对象
- Java线程同步(synchronized)——卖票问题
- Java线程同步 (synchronized wait notify)
- java线程同步(synchronized,wait,notify,notifyAll)