线程并发问题和线程的休眠

1、并发安全性问题

互联网的项目中存在这大量的并发案例,如卖火车票,电商网站等

范例:火车票有100张票,4个窗口同时卖票

1)采用继承Thread来实现

package com.cn.tickets;
/**
 * 卖票窗口
 * @author Administrator
 *
 */
public class SaleTicket extends Thread {

	private String name;
	
	
	
	public SaleTicket(String name) {
		super(name);
		
	}

	//100张票是共享的资源
	private static int tickets = 100;
	//类属性,同步锁(同步钥匙)对象
	private static Object obj = new Object();
	
	@Override
	public void run() {
		while(true){
			synchronized(obj){
				//同步代码块
				if(tickets > 0){
					try {
						//休眠时间
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(this.getName()+"正在卖第:"+tickets--+"张票");
				}else{
					System.out.println("票已经售完");
					break;
				}
				
			}
			
		}
	}

	
	
}
package com.cn.tickets;

public class TicketTest {
	
	public static void main(String[] args) {
		
		SaleTicket st1 = new SaleTicket("窗口1");
		SaleTicket st2 = new SaleTicket("窗口2");
		SaleTicket st3 = new SaleTicket("窗口3");
		SaleTicket st4 = new SaleTicket("窗口4");
		
		st1.start();
		st2.start();
		st3.start();
		st4.start();
		
	}

}

2)Runable接口

package com.cn.tickets2;
/**
 * 卖票窗口
 * @author Administrator
 *
 */
public class SaleTicket implements Runnable {

	//100张票是共享的资源
	private int tickets = 100;
	
	//private Object = new Object();
	
	
	@Override
	public void run() {
		while(true){

			int i = 0;
			if(i%2 == 0){
				
				//同步代码块
				synchronized(this){
					if(tickets > 0){
						try {
							//休眠时间
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName()+"正在卖第:"+tickets--+"张票");
					}else{
						System.out.println("票已经售完");
						break;
					}
				}
			}else{
				
				saleTickets();
			
			}
			i++;
			
			
		}
	

	}	
	
		

/**
 * 非静态方法的synchronized锁是当前对象
 */
public synchronized  void saleTickets(){

		if(tickets > 0){
			try {
				//休眠时间
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"正在卖第:"+tickets--+"张票");
		}else{
			System.out.println("票已经售完");
			
			}
	}
}
package com.cn.tickets2;

public class TicketTest {
	
	public static void main(String[] args) {
		
		SaleTicket st1 = new SaleTicket();
		
		Thread t1 = new Thread(st1,"窗口1");
		Thread t2 = new Thread(st1,"窗口2");
		Thread t3 = new Thread(st1,"窗口3");
		Thread t4 = new Thread(st1,"窗口4");
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		
	}

}

2、线程的休眠

线程的休眠是很必要的,在做服务端的时候为了减少服务器的压力,我们需要休眠,如果休眠是在同步代码块中执行,休眠不会让出锁对象

package com.cn.sleep;
/**
 * 卖票窗口
 * @author Administrator
 *
 */
public class SaleTicket implements Runnable {

	//100张票是共享的资源
	private int tickets = 100;
	
	//private Object = new Object();
	
	
	@Override
	public void run() {
		while(true){

				
				//同步代码块
				synchronized(this){
					if(tickets > 0){
						try {
							
							//休眠时间
							Thread.sleep(1000);
							System.out.println("-------------");
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName()+"正在卖第:"+tickets--+"张票");
					}else{
						System.out.println("票已经售完");
						break;
					}
				}
			}
		}
	}
		
package com.cn.sleep;

public class SleepTest {

	public static void main(String[] args) {
		/**
		SleepDemo sd = new SleepDemo();
		Thread t = new Thread(sd);
		
		t.start(); 
		*/
		
		SaleTicket st = new SaleTicket(); 
		Thread t1 = new Thread(st,"窗口1");
		Thread t2 = new Thread(st,"窗口2");
		
		t1.start();
		 t2.start();
		
	}
	
}



你可能感兴趣的:(java学习)