线程同步

当多个线程要使用同一个变量的时候

我们可以用 synchronized 修饰方法,他规定了一个线程完全执行完才可以让别的线程执行

import java.io.*;
import java.util.Scanner;
import java.sql.*;

public class Test {
    public static void main(String args[]){
        Demo demo1=new Demo();
        demo1.student=new Thread(demo1);
        demo1.teacher=new Thread(demo1);
        demo1.student.start();
        demo1.teacher.start();
    }
}

class Demo implements Runnable{
    Thread student,teacher;
    int money=0;
    @Override
    public void run() {
        // TODO 自动生成的方法存根
        if(Thread.currentThread()==student){
            work(100);
        }
        else if(Thread.currentThread()==teacher){
            work(100);
        }
    }
    synchronized void work(int num){
        if(Thread.currentThread()==student){
            for(int i=0;i<3;i++){
                money+=num;
                System.out.println("存入了"+money+"休息一会");
            }
        }
        else if(Thread.currentThread()==teacher){
            for(int i=0;i<3;i++){
                money-=num;
                System.out.println("存入了"+money+"休息一会");
            }
        }
    }
}
View Code

但在实际情况中又不是这死板,所以有了wait(),notify(),notifyAll()的方法

这都是Object类中的final方法,不是线程方法,可以直接wait(),notifyAll()地用

实际问题中wait()方法应当放在一个for循环而不是if()中

import java.io.*;
import java.util.Scanner;
import java.sql.*;

public class Test {
    public static void main(String args[]){
        Demo demo1=new Demo();
        demo1.student=new Thread(demo1);
        demo1.teacher=new Thread(demo1);
        demo1.student.setName("张飞");
        demo1.teacher.setName("李逵");
        demo1.teacher.start();
        demo1.student.start();
    }
}

class Demo implements Runnable{
    Thread student,teacher;
    int five=2,ten=0,twenty=0;
    @Override
    public void run() {
        // TODO 自动生成的方法存根
        if(Thread.currentThread()==student){
            work(5);
        }
        else if(Thread.currentThread()==teacher){
            work(20);
        }
    }
    synchronized void work(int num){
        if(num==5){
            five+=1;
            System.out.println(Thread.currentThread().getName()+"钱正好可以滚了");
            if(five==3)
                notifyAll();
        }
        else if(num==20){
            for(;five<3;){
                System.out.println("没有零钱");
                try{
                    wait();
                }
                catch(Exception e){
                    
                }
            }
            System.out.println("得到零钱滚吧");
        }
    }
}
View Code

join方法可以一个线程中加入另外一个线程,要等新线程完全执行后才执行会原来的线程

import java.io.*;
import java.util.Scanner;
import java.sql.*;

public class Test {
    public static void main(String args[]){
        Demo demo1=new Demo();
        demo1.student=new Thread(demo1);
        demo1.teacher=new Thread(demo1);
        demo1.student.setName("张飞");
        demo1.teacher.setName("李逵");
//        demo1.teacher.start();
        demo1.student.start();
    }
}

class Demo implements Runnable{
    Thread student,teacher;
    int five=2,ten=0,twenty=0;
    @Override
    public void run() {
        // TODO 自动生成的方法存根
        if(Thread.currentThread()==student){
            System.out.println("顾客开始买蛋糕");
            try{
                teacher.start();
                teacher.join();
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
            System.out.println("买了走人");
        }
        else if(Thread.currentThread()==teacher){
            System.out.println("师傅开始做蛋糕");
            try{
                teacher.sleep(4000);
            }
            catch(Exception e){
                System.out.println(e.toString());
            }
            System.out.println("做好了");
        }
    }
//    synchronized void work(int num){}
}
View Code

 

posted on 2016-11-21 10:46  Kooing  阅读(150)  评论(0编辑  收藏  举报

导航