Java多线程实现售票

Java多线程实现售票

  • 概述
  • 线程不安全
  • 线程安全

概述

马老师多线程视频学习总结(好记性不如烂笔头)。Java多线程实现售票。

线程不安全

package com.wz.code.test.thread;

import java.util.ArrayList;
import java.util.List;

public class TicketSeller1 {

    static List<String> tickets = new ArrayList<>();

    static {
        for (int i = 0; i < 1000; i++)
            tickets.add("票编号, " + i);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                while (tickets.size() > 0) {
                    System.out.println("销售了.." + tickets.remove(0));
                }
            }).start();;
        }
    }
}
package com.wz.code.test.thread;

import java.util.Vector;
import java.util.concurrent.TimeUnit;

public class TicketSeller2 {

    static Vector<String> tickets = new Vector<>();

    static {
        for (int i = 0; i < 1000; i++)
            tickets.add("票编号, " + i);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                // size 原子性
                while (tickets.size() > 0) {
                    // 以下代码不是原子性
                    try {
                        TimeUnit.MICROSECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    // remove 原子性
                    System.out.println("销售了.." + tickets.remove(0));
                }
            }).start();;
        }
    }
}

线程安全

package com.wz.code.test.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TicketSeller3 {

    static List<String> tickets = new ArrayList<>();

    static {
        for (int i = 0; i < 1000; i++)
            tickets.add("票编号, " + i);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                while (true) {
                    synchronized (tickets) {
                        if (tickets.size() <= 0)
                            break;
                        try {
                            TimeUnit.MICROSECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println("销售了.." + tickets.remove(0));
                    }
                }
            }).start();;
        }
    }
}


package com.wz.code.test.thread;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class TicketSeller4 {

    static Queue<String> tickets = new ConcurrentLinkedQueue<String>();

    static {
        for (int i = 0; i < 1000; i++)
            tickets.add("票编号, " + i);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                while (true) {
                    String s = tickets.poll();
                    // 判断之后不再操作队列
                    if (s == null)
                        break;
                    else
                        System.out.println("销售了.." + s);
                }
            }).start();
        }
    }
}

你可能感兴趣的:(java,多线程)