您的位置:首页 > 编程语言 > Java开发

JAVA多线程基础知识复习二

2017-07-08 16:25 525 查看
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 sy
a12f
nchronized 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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: