把synchronized当作函数修饰符时,示例代码如下:

  

Public synchronized void method(){
//….
}  

  这也就是同步方法,那这时synchronized锁定的是哪个对象呢?他锁定的是调用这个同步方法对象。也就是说,当一个对象P1在不同的线程中执行这个同步方法时,他们之间会形成互斥,达到同步的效果。但是这个对象所属的Class所产生的另一对象P2却能够任意调用这个被加了synchronized关键字的方法。

  如同这样

public void method()
{
synchronized (this)      //  (1)
{
       //…..
}
}   

  此处的this指的是什么呢?他指的就是调用这个方法的对象,如P1。可见同步方法实质是将synchronized作用于object reference。――那个拿到了P1对象锁的线程,才能够调用P1的同步方法,而对P2而言,P1这个锁和他毫不相干,程式也可能在这种情形下摆脱同步机制的控制,造成数据混乱。具体使用像这样:

  

package com.java.Thread;

public class Mysynchronized {
    /**
     * @param args
     */
    public static void main(String[] args) {
        /*
         * GetTickets gt1 = new GetTickets(); GetTickets gt2 = new GetTickets();
         * GetTickets gt3 = new GetTickets(); gt1.setName("窗口一");
         * gt2.setName("窗口二"); gt3.setName("窗口三"); gt1.start(); gt2.start();
         * gt3.start();
         */
        GetTickets2 gt = new GetTickets2();
        Thread th1 = new Thread(gt, "窗口一");
        Thread th2 = new Thread(gt, "窗口二");
        Thread th3 = new Thread(gt, "窗口三");
        th1.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        gt.flag = true;
        th2.start();
        th3.start();
    }

}

class GetTickets2 implements Runnable {

    private int tickets = 10;
    boolean flag = false;
 /*   Object ob = new Object();*/
    public void run() {
        if (flag) {
            for (int i = 0; i < 10; i++) {
                //synchronized (ob) {//如果用ob就无法同步
                synchronized (this) {
                    if (tickets > 0) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()
                                + "卖出" + (tickets--) + "号票"+":同步代码块");
                    }
                }

            }

        } else {
            for (int i = 0; i < 10; i++) {
                function();

            }

        }
    }

    public synchronized void function() {

        if (tickets > 0) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "卖出"
                    + (tickets--) + "号票"+":同步函数");

        }
    }

}
/*
 * class GetTickets extends Thread{ //private static int tickets = 10; private
 * int tickets = 10; public void run(){
 *
 * for (int i = 0; i < 10; i++) { if(tickets>0){
 * System.out.println(Thread.currentThread().getName()+"卖出"+(tickets--)+"号票"); }
 * } } }
 */

  将synchronized作用于static 函数 同步的锁就是它自己本身的字节码,示例代码如下:

     Class Foo
{
public synchronized static void method1()   // 同步的static 函数
{
//….
}
public void method2()
{
       synchronized(Foo.class)   //  class literal(类名称字面常量)
}
       }   

具体代码如下

package cn.java.thread;

/*
证明同步函数用的是this这把锁
*/
public class Tickets1 {

/**
* @param args
*/
public static void main(String[] args) {
/*
* GetTickets gt1 = new GetTickets(); GetTickets gt2 = new GetTickets();
* GetTickets gt3 = new GetTickets(); gt1.setName("窗口一");
* gt2.setName("窗口二"); gt3.setName("窗口三"); gt1.start(); gt2.start();
* gt3.start();
*/
GetTickets2 gt = new GetTickets2();
Thread th1 = new Thread(gt, "窗口一");
Thread th2 = new Thread(gt, "窗口二");
Thread th3 = new Thread(gt, "窗口三");
th1.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gt.flag = true;
th2.start();
th3.start();
}

}

class GetTickets2 implements Runnable {

private static int tickets = 10;
boolean flag = false;
Object ob = new Object();
public void run() {
if (flag) {
for (int i = 0; i < 10; i++) {
//synchronized (this.getClass()) {
synchronized (GetTickets2.class) {
if (tickets > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "卖出" + (tickets--) + "号票"+":同步代码块");
}
}

}

} else {
for (int i = 0; i < 10; i++) {
function();

}

}
}

public static synchronized void function() {

if (tickets > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出"
+ (tickets--) + "号票"+":同步函数");

}
}

}
/*
* class GetTickets extends Thread{ //private static int tickets = 10; private
* int tickets = 10; public void run(){
*
* for (int i = 0; i < 10; i++) { if(tickets>0){
* System.out.println(Thread.currentThread().getName()+"卖出"+(tickets--)+"号票"); }
* } } }
*/

然后嘞是 wait notify notifyall 这三个(两个)方法的 理解了 第一步 wait的作用是阻塞 notify的作用是唤醒

  讲这个 要用到以前学到的生产者和消费者 对于他们的理解:

  1.这些方法都存在与同步中

  2;使用这些方法时必须要标记所属的同步锁

  3:锁可以是任意对象,所以任意对象调用的方法一定定义在object类中  废话不多上代码:

  这是同步代码块的

package com.java.Thread;
//定义一个rbq
class Production{
    private String name;
    private String country;
    private boolean flag = true;
    public boolean isFlag() {
        return flag;
    }
    public void setFlag(boolean flag) {
        this.flag = flag;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

}
class Producer implements Runnable{
    private Production p = null;

    public Producer(Production p) {
        this.p = p;
    }
    public void run(){
        boolean b = true;
        while(true){
            synchronized (p) {
                if(p.isFlag()){
                    //这个b就是打酱油的
                    if(b){
                        p.setName("李小文院士");
                        p.setCountry("中国");
                        b = false;
                    }
                    else{
                        p.setName("加来道雄博士");
                        p.setCountry("日本");
                        b = true;
                    }
                    //唤醒 并且这里注意 把flag改变成了 false
                    p.setFlag(false);
                    p.notify();
                }
                else
                    try {
                    //老样子 否则就过来阻塞
                        p.wait();
                    } catch (Exception e) {
                        // TODO: handle exception
                    }

            }

        }
    }

}
class Saler implements Runnable{
    private Production p = null;
        //表明同步的锁!!!!
    public Saler(Production p) {
        this.p = p;
    }

    public void run() {
        while(true){
            synchronized (p) {
                //上面的 isFlage被设定成 false了 所以这里要不等于 然后他们 就这样一直轮回下去 下面没了就让上面造
                if(!p.isFlag()){
                    //获取前面的 赋值
                    System.out.println(p.getName()+":"+p.getCountry());
                    p.setFlag(true);
                    //唤醒 动起来
                    p.notify();
                }
                else{
                    try {
                        //阻塞
                        p.wait();
                    } catch (Exception e) {
                        // TODO: handle exception
                    }

                }

            }

        }

    }

}

public class ProductionDemo{
public static void main(String[] args) {
        Production p = new Production();
        Producer producer = new Producer(p);
        Saler saler = new Saler(p);
        Thread th1 = new Thread(producer);
        Thread th2 = new Thread(saler);
        th1.start();
        th2.start();

        }
        }

小钢炮这个是同步函数的

public class ProductionDemo{
public static void main(String[] args) {
        // 这是同步函数的
       /* Production p = new Production();
        Producer producer = new Producer(p);
        Saler saler = new Saler(p);
        Thread th1 = new Thread(producer);
        Thread th2 = new Thread(saler);
        th1.start();
        th2.start();
        */
        //同步代码块
    Production p = new Production();
    Producer pd = new Producer(p);
    Saler s = new Saler(p);
    Thread th1 = new Thread(pd);
    Thread th2 = new Thread(s);
    th1.start();
    th2.start();

}
}

//同步代码块
class Production {
    private String name;
    private int num = 0;
    private boolean b = false;

    public boolean isB() {
        return b;
    }

    public void setB(boolean b) {
        this.b = b;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

}

class Producer implements Runnable {

    private Production p = null;

    public Producer(Production p) {
        this.p = p;
    }

    public void run() {
        while (true) {
            synchronized (p) {
                if (p.isB()) {
                    try {
                        p.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                p.setName("小钢炮");
                p.setNum(p.getNum() + 1);
                System.out.println("生产:" + p.getName() + p.getNum());
                p.setB(true);
                p.notify();

            }

        }

    }

}

class Saler implements Runnable {

    private Production p = null;
    //获取定义的锁要一样
    public Saler(Production p) {
        this.p = p;
    }

    public void run() {
        while (true) {
            synchronized (p) {
                if (!p.isB()) {
                    try {
                        //阻塞
                        p.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("卖:" + p.getName() + p.getNum());
                p.setB(false);
                //唤醒
                p.notify();
            }
        }
    }

}

然后了是多生产者和多消费者 嘿嘿上码:

package cn.java.thread1;

public class ProductionDemo2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Production2 p = new Production2();
        Producer2 pd = new Producer2(p);
        Saler2 s = new Saler2(p);
        Thread th1 = new Thread(pd);
        Thread th2 = new Thread(pd);
        Thread th3 = new Thread(s);
        Thread th4 = new Thread(s);
        Thread th5 = new Thread(pd);
        Thread th6 = new Thread(s);
        th1.start();
        th2.start();
        th3.start();
        th4.start();
        th5.start();
        th6.start();

    }

}

class Production2 {
    private String name;
    private int num = 0;
    private boolean b = false;

    public boolean isB() {
        return b;
    }

    public void setB(boolean b) {
        this.b = b;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public synchronized void produce() {
        //if (b) {
        while (b) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        setName("小钢炮");
        setNum(getNum() + 1);
        System.out.println("生产:" + getName() + getNum());
        setB(true);
        //notify();
        notifyAll();
    }

    public synchronized void sale() {
        //if (!b) {
        while(!b){
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        System.out.println("卖:" + getName() + getNum());
        setB(false);
        //notify();
        notifyAll();

    }
}

class Producer2 implements Runnable {

    private Production2 p = null;

    public Producer2(Production2 p2) {
        this.p = p2;
    }

    public void run() {
        for (int i = 0; i < 100; i++) {
            p.produce();

        }

    }

}

class Saler2 implements Runnable {

    private Production2 p = null;

    public Saler2(Production2 p2) {
        this.p = p2;
    }

    public void run() {
        for (int i = 0; i < 100; i++) {
            p.sale();
        }

    }

}

最新文章

  1. The Basics of 3D Printing in 2015 - from someone with 16 WHOLE HOURS' experience
  2. C# async await 学习笔记1
  3. nyoj 102 次方求摸 快速幂
  4. Leetcode#126 Word Ladder II
  5. 在Hadoop1.2.1分布式集群环境下安装hive0.12
  6. 执行umount 命令的时候出现 device is busy
  7. Eclipse开发Android报错Jar mismatch! Fix your dependencies
  8. 【3D研发笔记】之【数学相关】(一):坐标系
  9. oracle整体知识的大致介绍(1)-概念
  10. 使用XML文件定义菜单
  11. 用js来实现那些数据结构(栈01)
  12. C#利用Attribute实现简易AOP介绍
  13. [React] 从零开始的react
  14. .Net分布式锁
  15. laravel用crud之index列出产品items
  16. Linux输入子系统 : 按键驱动
  17. Hadoop2源码分析-RPC机制初识
  18. 洛谷 P2146 [NOI2015]软件包管理器 解题报告
  19. 【ARM】2410裸机系列-uart串口通信
  20. Codeforces 607A 动态规划

热门文章

  1. jvm栈-运行控制,jvm-堆运行存储共享单元
  2. Day2_元组_字典_集合_字符编码_文件处理
  3. 区块链共识机制(POW、POS、DPOS等)的优缺点
  4. Windows下MySQL重装引起问题的解决
  5. Angularjs $http服务的两个request安全问题
  6. Commandline OpenVPN client on Mac OSX with macports
  7. Hello Django
  8. Java多线程问题
  9. form表单发送请求实例
  10. EXCEL解析之终极方法WorkbookFactory