Multi-threaded code 02 practice

A second way to create multiple threads: Implement Runnalble Interface

Thread-safety issues that may arise

  1. The reason Casual thread safety problems: When there are multiple threads share data, due to the operation of one thread to shared data has not been completed, has been involved in other threads out
    2. How to solve the security problems thread: a thread operations to ensure that the party shared data when the other threads must wait outside. Know the operation of executing the threads share data, other threads can operate contribution data
    3. How to operate: ② ①java how to achieve what is shared data
    in 4.java how to achieve security thread: ① through the use of synchronized synchronized block ② use synchronization method.

Synchronization code block synchronized manner


//1. 创建实现Runnable接口的类
class Ticket1 implements  Runnable{
    int ticket =100;
    Object obj = new Object(); //必须定义到run方法以外。类似wc的灯
    //2. 重写Runnable接口中的抽象方法
    public  void run(){
        while(true){
            //同步代码块的方式
            synchronized (obj) {    //obj同步监视器 或者直接写this(当前对象)
                if (ticket > 0) {
                    try{
                        Thread.currentThread().sleep(100);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "售票,票号位:" + ticket--);
                }
            }
        }
    }
}
public class TestTicket1 {
    public static void main(String[] args) {
        //3. 创建一个实现接口的子类对象
        Ticket1 t = new Ticket1();
        //4. 将创建的子类对象作为形参传递给Thread类的构造器
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        Thread t3 = new Thread(t);

        t1.setName("t1窗口");
        t2.setName("t2窗口");
        t3.setName("t3窗口");

        t1.start();
        t2.start();
        t3.start();
    }
}

Synchronized mode synchronization method

//1. 创建实现Runnable接口的类
class Ticket1 implements  Runnable{
    int ticket =100;
    Object obj = new Object(); //必须定义到run方法以外。类似wc的灯
    //2. 重写Runnable接口中的抽象方法
    public  void run(){
        while(true){
            show();
        }
    }

    //同步方法的方式  加synchronized 关键字
    public synchronized void show(){   //同步方法默认的锁是当前对象
        if (ticket > 0) {
            try{
                Thread.currentThread().sleep(100);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "售票,票号位:" + ticket--);
        }
    }
}
public class TestTicket1 {
    public static void main(String[] args) {
        //3. 创建一个实现接口的子类对象
        Ticket1 t = new Ticket1();
        //4. 将创建的子类对象作为形参传递给Thread类的构造器
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        Thread t3 = new Thread(t);

        t1.setName("t1窗口");
        t2.setName("t2窗口");
        t3.setName("t3窗口");

        t1.start();
        t2.start();
        t3.start();
    }
}

Guess you like

Origin www.cnblogs.com/liyao0312/p/11622466.html