JAVA多线程基础知识复习二

class Clent1{
    ***private*** int money=500;
    public ***synchronized*** void getMoney(int number){
        if(money<0){
            System.out.println("您的余额为零!");
        }else if(money<number){
            System.out.println("您余额不足");
        }else{
            money-=number;
            System.out.println("您取钱"+number+"剩余"+money+"元");
        }
    }
}
class MyThreaed4 extends Thread{
    public Clent1 clent=null;
    public MyThreaed4(){}
    public MyThreaed4(Clent1 clent){
        this.clent=clent;
    }
    @Override
    public void run() {
        clent.getMoney(400);
    }

}
如果不用synchronized关键字,就会取两次钱,出错

实现Runable接口:

public class Demo3 {
public static void main(String[] args) {
MyThreaed3 mth=new MyThreaed3();
Thread th1=new Thread(mth);
Thread th2=new Thread(mth);
th1.start();
try {
th1.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
th2.start();
}
}
class Clent{
private int money=500;
public void getMoney(int number){
if(money<0){
System.out.println(“您的余额为零!”);
}else if(){
System.out.println(“您余额不足”);
}else{
money-=number;
System.out.println(“您取钱”+number+”剩余”+money+”元”);
}
}
}
class MyThreaed3 implements Runnable{
Clent clent=new Clent();
public MyThreaed3(){}
public MyThreaed3(Clent clent){
this.clent=clent;
}
@Override
public void run() {
clent.getMoney(400);
}
}
用实现runable接口比继承Thread的要常用,并且对于同步安全性比继承要好,可以避免多继承的问题。

(6) 线程死锁 :
对象一有对象一的锁,要再拿对象二的所 对象二有对象二的锁,要在拿对象一的锁
具体代码中问题:
public class SiSuo {
public static void main(String[] args) {
People peo=new People();
ZhangHu zh1=new ZhangHu(peo);
ZhangHu1 zh2=new ZhangHu1(peo);
zh1.start();
zh2.start();
}
}
class ZhangHu extends Thread{
private People people=null;
public ZhangHu(){}
public ZhangHu(People people){
this.people=people;
}
@Override
public void run() {
people.getMethed1();
}
}
class ZhangHu1 extends Thread{
private People people=null;
public ZhangHu1(){}
public ZhangHu1(People people){
this.people=people;
}
@Override
public void run() {
people.getMethed2();
}
}
class People{
private Object ob1=new Object();
private Object ob2=new Object();
public void getMethed1(){
synchronized (ob1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (ob2) {
System.out.println(“方法一”);
}
}
}
public void getMethed2(){
synchronized (ob2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (ob1) {
System.out.println(“方法二”);
}
}
}
}
(6) 线程通信—–消费者与生产者的问题
import java.util.LinkedList;
public class ProductCustomerDemo {
public static void main(String[] args) {
Basket bas=new Basket();
ProductThread pt=new ProductThread(bas);
CustomerThread ct=new CustomerThread(bas);
pt.start();
ct.start();
}
}
class ProductThread extends Thread{
private Basket basket=null;
public ProductThread(Basket basket){
this.basket=basket;
}
@Override
public void run() {
basket.pushApple();
}
}
class CustomerThread extends Thread{
private Basket basket=null;
public CustomerThread(Basket basket){
this.basket=basket;
}
@Override
public void run() {
basket.popApple();
}
}
class Basket{
private LinkedList list=new LinkedList();
public synchronized void pushApple(){
for(int i=0;i<20;i++){
Apple apple=new Apple(i);
push(apple);
}
}
public synchronized void popApple(){
for(int i=0;i<20;i++){
Apple apple=new Apple(i);
pop(apple);
}
}
public void push(Apple apple){
if(list.size()==5){
try {
wait(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
list.addFirst(apple);
System.out.println(“已经添加”+apple.toString());
notify();
}
public void pop(Apple apple){
if(list.size()==0){
try {
wait(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
list.removeFirst();
System.out.println(“已经消费”+apple.toString());
notify();
}
}
class Apple{
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Apple(){}
public Apple(int id) {
super();
this.id = id;
}
@Override
public String toString() {
return “苹果”+(id+1);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值