Java 基础 线程的Runnable接口 /线程的同步方法 /同步代码块

笔记:

/**通过 Runnable接口来实现多线程
 *      1. 创建一个实现runnable 接口的类
 *      2. 在类中实现接口的run() 抽象方法
 *      3. 创建一个runnable 接口实现类的对象
 *      4. 将此对象作为形参传递给Thread 类的构造器中  ,创建 Thread 线程
 *      5.调用start() 方法,执行 Thread 方法
 *  ------------------------------
 *  Runnable接口意义:
 *      可以应用于 已经继承与其他类的线程, 毕竟一个类只能继承一个类!
 *      class  className  extends  FatherClass  implements  Runnable
 *
 */
/**模拟火车站开启三个窗口售票,火车站总票数20张!
 *      1.此线程存在安全问题, 打印车票时,会出现重票和错票问题!!
 *      2.如何保证线程的安全?
 *        方法一:同步代码块,
 *          synchronized(同步监视器){
 *              //需要被同步的代码块(即为操作共享 数据的代码)
 *          }
 *          (1) 共享数据, 多个线程 共同操作的同一个数据(变量)
 *          (2) 同步监视器 ,由一个类的对象(任意对象即可!!)来充当;
 *                  哪个线程获取此监视器,谁就执行大括号里被同步的代码
 *        方法二:同步方法
 *              public synchronized void methods() {}即可!
 */

 

线程的Runnable接口 代码

class PrintNum implements Runnable{
    @Override
    public void run() {
        for(int i=1;i<=20;i++)
        {
            System.out.println(Thread.currentThread().getName()+" "+i);
        }
    }
}
public class TestRunnable {
    public static void main(String[] args) {
        PrintNum p=new PrintNum();
        Thread t1=new Thread(p);
        Thread t2=new Thread(p);
        t1.start();//启动线程:执行Thread 对象生成时,构造器形参的对象的run()方法
        t2.start();
    }
}

 

线程的同步方法 代码

//将操作共享数据的方法声明为synchronized ,即此方法为同步方法,能保证当其中一个线程执行此方法时,其他线程在外等待直至此
// 线程全部执行完毕

class Window implements Runnable{  //synchronized( 同步方法 ){} 的方式
    public static int tickets=30;

    public void run() {
        while(tickets>0){
            print_num();
        }
    }
    public synchronized void print_num( ){  //synchronized( 同步方法 ){} 的方式
        if(tickets>0){
            try {
                Thread.currentThread().sleep(10);  //休息10ms,相当于售票花费的时间!
                tickets--;
                System.out.println(Thread.currentThread().getName()+" 为您售票!"+
                        "剩余票数:"+tickets);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

同步代码块 代码

class Window2 implements Runnable{     //synchronized(同步监视器){} 的方式
    int tickets=30;   //全局变量了
    Object obj=new Object();
    public  void run() {
        while(true){
            synchronized(obj) {
                if (tickets > 0) {
                    try {
                        Thread.currentThread().sleep(10);  //休息10ms,相当于售票花费的时间!
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    tickets--;
                    System.out.println(Thread.currentThread().getName() + " 为您售票!" +
                            "剩余票数:" + tickets);
                } else
                    break;     //记得在这里解除死循环!!
            }
        }
    }
}
public class TestWindows {
    public static void main(String[] args) {
        Window w=new Window();

        Thread w1=new Thread(w);
        Thread w2=new Thread(w);
        Thread w3=new Thread(w);

        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");
        w1.start();
        w2.start();
        w3.start();
    }

}

 

posted @ 2018-10-18 15:18  山枫叶纷飞  阅读(711)  评论(0编辑  收藏  举报