线程之间共享数据(两个线程之间使用同一份数据),线程锁要统一,不统一的话会出现线程安全问题...

 

1.任意对象锁

public class ThisLockDemo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Ticket5 ticket = new Ticket5();
		Thread t = new Thread(ticket);
		t.start();
		try {Thread.sleep(100);} catch (Exception e) {}
		ticket.flag = false;
		Thread t2 = new Thread(ticket);
		t2.start();

	}

}

class Ticket5 implements Runnable {
	private int tickets = 100;
	Object obj = new Object();
	boolean flag = true;

	@Override
	// public synchronized void run() {
	public void run() {
		if (flag) {
			while (true) {
				synchronized (obj) { //任意对象锁
					if (tickets > 0) {
						try {
							Thread.sleep(10);
						} catch (Exception e) {
						}
						System.out.println(Thread.currentThread().getName()+"-----"+tickets--);
					}
				}
			}
		}else{
			while(true){
				show();
			}
		}
	}

	public synchronized void show() {
		if (tickets > 0) {
			try {Thread.sleep(10);} catch (Exception e) {}
			System.out.println(Thread.currentThread().getName()+"+++++"+tickets--);
		}
	}
}

  输出结果:

      

Thread-0-----100
Thread-0-----99
Thread-0-----98
Thread-0-----97
Thread-0-----96
Thread-0-----95
Thread-0-----94
Thread-0-----93
Thread-0-----92
Thread-0-----91
Thread-0-----90
Thread-1+++++89
Thread-0-----87
Thread-1+++++88
Thread-1+++++86
Thread-0-----85
Thread-1+++++84
Thread-0-----83
Thread-1+++++82
Thread-0-----81
Thread-1+++++80
Thread-0-----79
Thread-1+++++78
Thread-0-----77
Thread-0-----76
Thread-1+++++76
Thread-1+++++75
Thread-0-----74
Thread-0-----73
Thread-1+++++72
Thread-0-----71
Thread-1+++++70
Thread-1+++++69
Thread-0-----68
Thread-1+++++67
Thread-0-----66
Thread-1+++++65
Thread-0-----64
Thread-1+++++63
Thread-0-----62
Thread-0-----61
Thread-1+++++60
Thread-1+++++59
Thread-0-----58
Thread-1+++++57
Thread-0-----56
Thread-1+++++55
Thread-0-----54
Thread-1+++++53
Thread-0-----52
Thread-0-----51
Thread-1+++++50
Thread-1+++++49
Thread-0-----48
Thread-1+++++47
Thread-0-----46
Thread-0-----45
Thread-1+++++44
Thread-1+++++43
Thread-0-----42
Thread-0-----41
Thread-1+++++40
Thread-0-----38
Thread-1+++++39
Thread-1+++++37
Thread-0-----36
Thread-0-----35
Thread-1+++++34
Thread-1+++++33
Thread-0-----32
Thread-0-----31
Thread-1+++++30
Thread-1+++++29
Thread-0-----28
Thread-1+++++27
Thread-0-----26
Thread-1+++++25
Thread-0-----24
Thread-1+++++23
Thread-0-----22
Thread-1+++++21
Thread-0-----20
Thread-1+++++19
Thread-0-----18
Thread-1+++++17
Thread-0-----16
Thread-1+++++15
Thread-0-----14
Thread-1+++++13
Thread-0-----12
Thread-1+++++11
Thread-0-----10
Thread-1+++++9
Thread-0-----8
Thread-1+++++7
Thread-0-----6
Thread-1+++++5
Thread-0-----4
Thread-1+++++3
Thread-0-----2
Thread-0-----1
Thread-1+++++0

  2.同一对象锁:不存在线程安全问题

    

package day11;

public class ThisLockDemo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Ticket5 ticket = new Ticket5();
		Thread t = new Thread(ticket);
		t.start();
		try {Thread.sleep(100);} catch (Exception e) {}
		ticket.flag = false;
		Thread t2 = new Thread(ticket);
		t2.start();

	}

}

class Ticket5 implements Runnable {
	private int tickets = 100;
	Object obj = new Object();
	boolean flag = true;

	@Override
	// public synchronized void run() {
	public void run() {
		if (flag) {
			while (true) {
				synchronized (this) { 
					if (tickets > 0) {
						try {
							Thread.sleep(10);
						} catch (Exception e) {
						}
						System.out.println(Thread.currentThread().getName()+"-----"+tickets--);
					}
				}
			}
		}else{
			while(true){
				show();
			}
		}
	}

	public synchronized void show() {  //此方法的对象锁是 this
		if (tickets > 0) {
			try {Thread.sleep(10);} catch (Exception e) {}
			System.out.println(Thread.currentThread().getName()+"+++++"+tickets--);
		}
	}
}

  输出结果:

Thread-0-----100
Thread-0-----99
Thread-0-----98
Thread-0-----97
Thread-0-----96
Thread-0-----95
Thread-0-----94
Thread-0-----93
Thread-0-----92
Thread-0-----91
Thread-0-----90
Thread-0-----89
Thread-0-----88
Thread-0-----87
Thread-0-----86
Thread-0-----85
Thread-0-----84
Thread-0-----83
Thread-0-----82
Thread-0-----81
Thread-0-----80
Thread-0-----79
Thread-0-----78
Thread-0-----77
Thread-0-----76
Thread-0-----75
Thread-0-----74
Thread-0-----73
Thread-0-----72
Thread-0-----71
Thread-0-----70
Thread-0-----69
Thread-0-----68
Thread-0-----67
Thread-0-----66
Thread-1+++++65
Thread-1+++++64
Thread-1+++++63
Thread-1+++++62
Thread-1+++++61
Thread-1+++++60
Thread-1+++++59
Thread-1+++++58
Thread-1+++++57
Thread-1+++++56
Thread-1+++++55
Thread-1+++++54
Thread-1+++++53
Thread-1+++++52
Thread-1+++++51
Thread-1+++++50
Thread-1+++++49
Thread-1+++++48
Thread-1+++++47
Thread-1+++++46
Thread-1+++++45
Thread-1+++++44
Thread-1+++++43
Thread-1+++++42
Thread-1+++++41
Thread-1+++++40
Thread-1+++++39
Thread-1+++++38
Thread-1+++++37
Thread-1+++++36
Thread-1+++++35
Thread-1+++++34
Thread-1+++++33
Thread-1+++++32
Thread-1+++++31
Thread-1+++++30
Thread-1+++++29
Thread-1+++++28
Thread-1+++++27
Thread-1+++++26
Thread-1+++++25
Thread-1+++++24
Thread-1+++++23
Thread-1+++++22
Thread-1+++++21
Thread-1+++++20
Thread-1+++++19
Thread-1+++++18
Thread-1+++++17
Thread-1+++++16
Thread-1+++++15
Thread-1+++++14
Thread-1+++++13
Thread-1+++++12
Thread-1+++++11
Thread-1+++++10
Thread-1+++++9
Thread-1+++++8
Thread-1+++++7
Thread-1+++++6
Thread-1+++++5
Thread-1+++++4
Thread-1+++++3
Thread-1+++++2
Thread-1+++++1

  

 

转载于:https://www.cnblogs.com/childhooding/p/4644643.html

你可能感兴趣的:(线程之间共享数据(两个线程之间使用同一份数据),线程锁要统一,不统一的话会出现线程安全问题...)