多线程

  

 

第二条增加:,2.Thread.sleep ()让当前线程休眠毫秒

       sleep:释放cpu抢先权,和锁旗标的监控权没有关系

 

package workhome;

public class ThreadDemo1 {
    public static void main(String[] args) {
        // 创建线程对象
        MyThread t1 = new MyThread();
        YourThread t2 = new YourThread();

        t1.start();
        t2.start();
    }
}
//线程1
class MyThread extends Thread {
        public void run() {
            for (;;) {
                System.out.println("hello world -1");
            }
        }
    
}

//线程2
class YourThread extends Thread {
        public void run() {
            for (;;) {
                System.out.println("hello world -2");
            }
    }
}
package workhome;

public class ThreadDemo1 {
    public static void main(String[] args) {
        // 创建线程对象
        MyThread t1 = new MyThread("Thread-1");
        MyThread t2 = new MyThread("Thread-2");
        
        t1.start();
        t2.start();
    }
}

//线程1
class MyThread extends Thread {
    private String name;

    public MyThread(String name) {
        this.name = name;
    }

    public void run() {
        for (;;) {
            System.out.println(name);
            //yield,放弃,谦逊
            Thread.yield();
        }
    }
}
package workhome;

public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {
        // 创建线程对象
        Player p1 = new Player("成龙",10000);
        Player p2 = new Player("李连杰",20000);
        Player p3 = new Player("史泰龙",25000);
        
        //
        p1.start();
        p2.start();
        p3.start();
        try {
            p1.join();
            p2.join();
            p3.join();
        }
        catch(Exception e) {
            
        }
        
        //
        System.out.println("开局");
    }
}

//线程1
class Player extends Thread {
    private String name;
    private int time;
    public Player(String name,int time) {
        this.name = name;
        this.time=time;
    }

    public void run() {
        System.out.println(name+"出发了");
        try {
            //
            Thread.sleep(time);
        } catch (Exception e) {
            // TODO: handle exception
        }
        System.out.println(name + "到了"+"耗时"+time);
    }
}

 

package workhome;

public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {
        // 创建线程对象
        Box no1 = new Box("No1",3000);
        Box no2 = new Box("No2",7000);
        Waiter w = new Waiter();
        
        //设置线程为守护线程
        w.setDaemon(true);
        no1.start();
        no2.start();
        w.start();
    }
}

//线程1
class Box extends Thread {
    private String no;
    private int time;
    public Box(String no,int time) {
        this.no = no;
        this.time=time;
    }

    public void run() {
        System.out.println(no + " 号包房开始消费了");
        try {
            Thread.sleep(time);
        }
        catch(Exception e) {
        }
        System.out.println(no+ "号包房消费时间:"+time+",结束消费");
    }
}

//服务员线程
class Waiter extends Thread{
    public void run() {
        while(true) {
            //打印当前系统时间
            System.out.println(new java.util.Date());
            try {
                Thread.sleep(500);
            }
            catch(Exception e) {
                
            }
        }
    }
}

 

package workhome;

public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {
        Saler s1 = new Saler("s1");
        Saler s2 = new Saler("s2");
        s1.start();
        s2.start();
    }
}

class Saler extends Thread {
    static int tickets = 100;
    // 锁旗标
    static Object lock = new Object();
    private String name;

    public Saler(String name) {
        this.name = name;
    }

    public void run() {

        while (true) {
            int t = getTicket();
            if (t == -1) {
                return;
            } else {
                System.out.println(name + " : " + t);
            }
        }
    }

    // 取票
    public int getTicket() {
        synchronized (lock) {
            int t = tickets;
            tickets = tickets - 1;
            return t < 1 ? -1 : t;
        }
    }
}

 

package workhome;

public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        Saler s1 = new Saler("s1",lock);
        Saler s2 = new Saler("s2",lock);
        s1.start();
        s2.start();
    }
}

class Saler extends Thread {
    static int tickets = 100;
    // 锁旗标
    Object lock;
    private String name;

    public Saler(String name,Object lock) {
        this.name = name;
        this.lock=lock;
    }

    public void run() {

        while (true) {
            int t = getTicket();
            if (t == -1) {
                return;
            } else {
                System.out.println(name + " : " + t);
            }
        }
    }

    // 取票
    public int getTicket() {
        synchronized (lock) {
            int t = tickets;
            try {
                //Thread.sleep(50);
            }
            catch (Exception e) {
                // TODO: handle exception
            }
            tickets = tickets - 1;
            return t < 1 ? -1 : t;
        }
    }
}

  

package workhome;

public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {

        TicketPool pool = new TicketPool();
        Saler s1 = new Saler("S-1", pool);
        Saler s2 = new Saler("S-2", pool);
        Saler s3 = new Saler("S-3", pool);
        Saler s4 = new Saler("S-4", pool);

        s1.start();
        s2.start();
        s3.start();
        s4.start();
    }
}

//售票员
class Saler extends Thread {

    private String name;
    private TicketPool pool;

    public Saler(String name, TicketPool pool) {
        this.name = name;
        this.pool = pool;
    }

    public void run() {
        while (true) {
            int no = pool.getTicket();
            if (no == 0) {
                return;
            } else {
                System.out.println(name + " : " + no);
                Thread.yield();
            }
        }
    }
}

//票池
class TicketPool {
    private int tickets = 10;

//
//    public int getTicket() { 
//        //同步代码块,以票池本身作为作为锁旗标 synchronized(this) {
//        int temp = tickets;
//        tickets = tickets-1;
//        return temp > 0 ? temp : 0;
//        }
//    }


    public synchronized int getTicket() {
        int temp = tickets;
        tickets = tickets - 1;
        return temp > 0 ? temp : 0;
    }
}

 

package workhome;

public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {

        Saler s1 = new Saler("S-1");
        Saler s2 = new Saler("S-2");
        Saler s3 = new Saler("S-3");
        Saler s4 = new Saler("S-4");

        s1.start();
        s2.start();
        s3.start();
        s4.start();
    }
}

//售票员
class Saler extends Thread {

    private String name;

    public Saler(String name) {
        this.name = name;
    }

    public void run() {
        while (true) {
            int no = TicketPool.getTicket();
            if (no == 0) {
                return;
            } else {
                System.out.println(name + " : " + no);
                Thread.yield();
            }
        }
    }
}

//票池
class TicketPool {
    private static int tickets = 10;
    public synchronized static int getTicket() {
        int temp = tickets;
        tickets = tickets - 1;
        return temp > 0 ? temp : 0;
    }
}

 

package workhome;

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

public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {
        Pool pool = new Pool();
        Productor p1 = new Productor("生产者1", pool);
        Productor p2 = new Productor("生产者2", pool);
        Consumer c1 = new Consumer("消费者1", pool);
        Consumer c2 = new Consumer("消费者2", pool);
        p1.start();
        p2.start();
        c1.start();
        c2.start();
    }
}

//生产者
class Productor extends Thread {
    static int i =0;
    private String name;
    private Pool pool;

    public Productor(String name, Pool pool) {
        this.name = name;
        this.pool = pool;
    }

    public void run() {
        //int i = 0;
        while (true) {
            pool.add(i++);
            try {
                Thread.sleep(50);
            }catch(Exception e) {
                e.printStackTrace();
            }
            System.out.println("add: " + i + " ");
        }
    }
}

//消费者
class Consumer extends Thread {
    private String name;
    private Pool pool;

    public Consumer(String name, Pool pool) {
        this.name = name;
        this.pool = pool;
    }

    public void run() {
        while (true) {
            int i = pool.remove();
            try {
                Thread.sleep(100);
            }catch(Exception e) {
                e.printStackTrace();
            }
            System.out.println("remove : " + i);
        }

    }
}

class Pool {
    private List<Integer> list = new ArrayList<Integer>();
    // 最大值
    private int MAX = 100;

    // 添加元素
    public void add(int n) {
        synchronized (this) {
            try {
                while(list.size() >= MAX) {
                    this.wait();
                    // System.out.println("xxxx");
                }
                    list.add(n);
                    System.out.println("size:"+list.size());
                    this.notify();
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    // 删除元素
    public int remove() {
        synchronized (this) {
            try {
                while (list.size() == 0) {
                    this.wait();
                }
                    int i = list.remove(0);
                    this.notify();
                    return i;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return -1;
        }
    }
}

 

posted @ 2019-05-25 20:15  殇之弑梦  阅读(111)  评论(0编辑  收藏  举报