线程Synchronized

public class Demo1_Synchronized {

/**
 * @param args
 * 同步代码块
 */
public static void main(String[] args) {
    final Printer p = new Printer();

    new Thread() {
        public void run() {
            while(true) {
                p.print1();
            }
        }
    }.start();

    new Thread() {
        public void run() {
            while(true) {
                p.print2();
            }
        }
    }.start();
}

}

class Printer {
Demo d = new Demo();
public void print1() {
//synchronized(new Demo()) { //同步代码块,锁机制,锁对象可以是任意的
synchronized(d) {
System.out.print(“1”);
System.out.print(“2”);
System.out.print(“3”);
System.out.print(“4”);
System.out.print(“5”);
System.out.print(“\r\n”);
}
}

public void print2() {
    //synchronized(new Demo()) {                            //锁对象不能用匿名对象,因为匿名对象不是同一个对象
    synchronized(d) {       
        System.out.print("A");
        System.out.print("B");
        System.out.print("C");
        System.out.print("D");
        System.out.print("\r\n");
    }
}

}

class Demo{}

public class Demo2_Synchronized {

/**
 * @param args
 * 同步代码块
 */
public static void main(String[] args) {
    final Printer2 p = new Printer2();

    new Thread() {
        public void run() {
            while(true) {
                p.print1();
            }
        }
    }.start();

    new Thread() {
        public void run() {
            while(true) {
                p.print2();
            }
        }
    }.start();
}

}

class Printer2 {
Demo d = new Demo();
//非静态的同步方法的锁对象是神马?
//答:非静态的同步方法的锁对象是this
//静态的同步方法的锁对象是什么?
//是该类的字节码对象
public static synchronized void print1() { //同步方法只需要在方法上加synchronized关键字即可
System.out.print(“1”);
System.out.print(“2”);
System.out.print(“3”);
System.out.print(“4”);
System.out.print(“5”);
System.out.print(“\r\n”);
}

public static void print2() {
    //synchronized(new Demo()) {                            //锁对象不能用匿名对象,因为匿名对象不是同一个对象
    synchronized(Printer2.class) {      
    System.out.print("A");
    System.out.print("B");
    System.out.print("C");
    System.out.print("D");
    System.out.print("\r\n");
    }
}

}

public class Demo3_Ticket {

/**
 * 需求:铁路售票,一共100张,通过四个窗口卖完.
 */
public static void main(String[] args) {
    new Ticket().start();
    new Ticket().start();
    new Ticket().start();
    new Ticket().start();
}

}

class Ticket extends Thread {
private static int ticket = 100;
//private static Object obj = new Object(); //如果用引用数据类型成员变量当作锁对象,必须是静态的
public void run() {
while(true) {
synchronized(Ticket.class) {
if(ticket <= 0) {
break;
}
try {
Thread.sleep(10); //线程1睡,线程2睡,线程3睡,线程4睡
} catch (InterruptedException e) {

                e.printStackTrace();
            }
            System.out.println(getName() + "...这是第" + ticket-- + "号票");
        }
    }
}

}

public class Demo4_Ticket {

/**
 * @param args
 * 火车站卖票的例子用实现Runnable接口
 */
public static void main(String[] args) {
    MyTicket mt = new MyTicket();
    new Thread(mt).start();
    new Thread(mt).start();
    new Thread(mt).start();
    new Thread(mt).start();

    /*Thread t1 = new Thread(mt);               //多次启动一个线程是非法的
    t1.start();
    t1.start();
    t1.start();
    t1.start();*/
}

}

class MyTicket implements Runnable {
private int tickets = 100;
@Override
public void run() {
while(true) {
synchronized(this) {
if(tickets <= 0) {
break;
}
try {
Thread.sleep(10); //线程1睡,线程2睡,线程3睡,线程4睡
} catch (InterruptedException e) {

                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "...这是第" + tickets-- + "号票");
        }
    }
}

}

public class Demo5_DeadLock {

/**
 * @param args
 */
private static String s1 = "筷子左";
private static String s2 = "筷子右";

public static void main(String[] args) {
    new Thread() {
        public void run() {
            while(true) {
                synchronized(s1) {
                    System.out.println(getName() + "...获取" + s1 + "等待" + s2);
                    synchronized(s2) {
                        System.out.println(getName() + "...拿到" + s2 + "开吃");
                    }
                }
            }
        }
    }.start();

    new Thread() {
        public void run() {
            while(true) {
                synchronized(s2) {
                    System.out.println(getName() + "...获取" + s2 + "等待" + s1);
                    synchronized(s1) {
                        System.out.println(getName() + "...拿到" + s1 + "开吃");
                    }
                }
            }
        }
    }.start();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值