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);
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章