多线程并发问题

线程的本质
    是 CPU的分片运行,不是真正的同时执行!是时间分片
    
多线程锁:
    锁方法
    锁对象
    锁成员变量
    synchronized(tickets);//不能锁基本数据类型 只能锁引用数据
    对象锁--又叫排他锁
    
synchronized(对象){}
synchronized 方法
    synchronized public void setAccount(double account) {}
synchronized(this){
//其他线程无法进入这个代码块
这个代码块,在某一个时间点,只有一个线程在运行
}
当一个线程给对象family 加上锁后,直到这个线程解锁前,其他线程无法再给该对象枷锁

数据库 web 服务器
如何锁 怎么锁 会产生什么样效果

守护线程案例

package com.icss.biz.ppt;

public class DaemonThread {

	public static void main(String[] args)  {
		Thread thread  = new Thread(new Runnable() { //定义了实现Runnable的匿名类

			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(500);
						System.out.println("-------- in daemon thread");
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
			
		});
		
		//修改参数为true 标记线程为Daemon Thread  /false 为user thread
		thread.setDaemon(true);
		thread.start();
		
		//如果虚拟机中只有Daemon thread 在运行则虚拟机退出
		for(int i = 0 ; i < 10 ; i++) {
			try {
				Thread.sleep(500);
				System.out.println("mian thread i="+i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("**********main thread end**************");
	}
}

案例:

父亲赚钱,妻子花钱 ,儿子花钱

package com.icss.biz;

public class Family {
	private Father father;
	private Wife wife;
	private Son son;
	private double account;//共用账户
	public Father getFather() {
		return father;
	}
	public void setFather(Father father) {
		this.father = father;
	}
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
	public Son getSon() {
		return son;
	}
	public void setSon(Son son) {
		this.son = son;
	}
	/**
	 * 方法上加锁
	 * 不能实现锁功能
	 * @return
	 */
	/*synchronized public double getAccount() {
		return account;
	}*/
	/**
	 * 方法上加锁
	 * @param account
	 */
	/*synchronized public void setAccount(double account) {
		this.account = account;
	}*/
	
	public double getAccount() {
		return account;
	}
	
	public void setAccount(double account) {
		this.account = account;
	}
}
package com.icss.biz;

public class Father  {

	private String name;
	private Family family; //类和类之间是组合关系
	
	public Father(String name,Family family) {
		this.name=name;
		this.family = family;
		this.family.setFather(this);
	}
	
	public String getName() {
		return name;
	}
	
	public Family getFamily() {
		return family;
	}
	
	public void earnMoney(double money) {
		synchronized(this.family){
			double leftMoney = this.family.getAccount(); //当前账户金额
			if(leftMoney < 0 ) {
				System.out.println("警告:leftMoney="+leftMoney +" ? ? ? ? ? ? ? ? ? ? ");
			}
			
			System.out.println("father充值前 : family account 剩余 "+ leftMoney);
			leftMoney  = this.family.getAccount() + money;
			this.family.setAccount(leftMoney); //回写金额
			System.out.println("father充值后 : family account 剩余 "+ leftMoney);
		}
		
		
	}
	
}
package com.icss.biz;

public class Wife {

	private String name;
	private Family family; //类和类之间是组合关系
	
	public Wife(String name,Family family) {
		this.name=name;
		this.family = family;
		this.family.setWife(this);
	}

	public String getName() {
		return name;
	}

	public Family getFamily() {
		return family;
	}

	public void shopping(double money) {
		synchronized(this.family){
			double leftMoney = this.family.getAccount(); //当前账户金额
			System.out.println("wife消费前 : family account 剩余 "+ leftMoney);
			try {
				Thread.sleep(80);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(money > leftMoney) {
				System.out.println("账户余额不足,请wife 暂停消费 -----------");
			}else {
				leftMoney  = this.family.getAccount() -money;
				this.family.setAccount(leftMoney); //回写金额
				System.out.println("wife消费后:family account 剩余 "+ leftMoney);
			}
		}
		
	}
}
package com.icss.ui;

import com.icss.biz.Family;
import com.icss.biz.Father;
import com.icss.biz.Son;
import com.icss.biz.Wife;

public class Test {

	public static void main(String[] args) {
		Family family = new Family();
		Father father = new Father("f",family);
		Wife wife = new Wife("w",family);
		Son son = new Son("s",family);
		new Thread(new FatherThread(father)).start();
		new Thread(new WifeThread(wife)).start();
		new Thread(new SonThread(son)).start();
	}

}

class FatherThread implements Runnable{
	private Father father ;
	public FatherThread(Father father ) {
		this.father = father;
	}
	@Override
	public void run() {
		while(true) {
			try {
				Thread.sleep(2000);
				father.earnMoney(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

class WifeThread implements Runnable{

	private Wife wife;
	public WifeThread(Wife wife) {
		this.wife = wife;
	}
	@Override
	public void run() {
		while(true) {
			try {
				Thread.sleep(1000);
				wife.shopping(7);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
}

class SonThread implements Runnable{
	 private Son son;
	 public SonThread(Son son) {
		 this.son = son;
	 }
	@Override
	public void run() {
		while(true) {
			try {
				Thread.sleep(1000);
				son.playGame(5);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值