多线程

来源:互联网 发布:小刀网软件 编辑:程序博客网 时间:2024/05/16 05:47

多线程

007
/**
* 同步和非同步方法是否可以同时调用? 可以
* @author mashibing
*/

public class T {    //同步对象  在实例方法上默认对象:this    public synchronized void m1() {         System.out.println(Thread.currentThread().getName() + " m1 start...");        try {            Thread.sleep(10000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + " m1 end");    }    //非锁    public void m2() {        try {            Thread.sleep(5000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + " m2 ");    }    public static void main(String[] args) {        T t = new T();          //第一种写法        /**          * 线程一:调用 实例t 的m1的方法(该锁就是 上面new 的对象this)          * 线程二:调用 实例t 的m2的方法:可以随时的调用m2          *           */        new Thread(()->t.m1(), "t1").start();        new Thread(()->t.m2(), "t2").start();        //第二种写法        new Thread(t::m1, "t1").start();        new Thread(t::m2, "t2").start();        //第三种写法        new Thread(new Runnable() {            @Override            public void run() {                t.m1();            }        });    }   }

008.
/**
* 对业务写方法加锁
* 对业务读方法不加锁
* 容易产生脏读问题(dirtyRead): 可以读到–>(正在写的数据,写还没结束)
*/

public class Account {    String name;    double balance;    public synchronized void set(String name, double balance) {        this.name = name;        /*        //主要为了展示出脏读的效果。让写的慢一点,读可以进到写的中间        try {            Thread.sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }        */        this.balance = balance;    }    //解决办法:记在读的方法也加入和写一样的对象锁    public /*synchronized*/ double getBalance(String name) {        return this.balance;    }    public static void main(String[] args) {        Account a = new Account();        new Thread(()->a.set("zhangsan", 100.0)).start();        try {            TimeUnit.SECONDS.sleep(1);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(a.getBalance("zhangsan"));        try {            TimeUnit.SECONDS.sleep(2);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(a.getBalance("zhangsan"));    }}

/**
* 一个同步方法可以调用另外一个同步方法,一个线程已经拥有某个对象的锁,再次申请的时候仍然会得到该对象的锁.
* 也就是说synchronized获得的锁是可重入的
* @author mashibing
*/

package yxxy.c_009;import java.util.concurrent.TimeUnit;public class T {    synchronized void m1() {        System.out.println("m1 start");        try {            TimeUnit.SECONDS.sleep(1);        } catch (InterruptedException e) {            e.printStackTrace();        }        m2();    }    synchronized void m2() {        try {            TimeUnit.SECONDS.sleep(2);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println("m2");    }}

/**
* 一个同步方法可以调用另外一个同步方法,一个线程已经拥有某个对象的锁,再次申请的时候仍然会得到该对象的锁.
* 也就是说synchronized获得的锁是可重入的
* 这里是继承中有可能发生的情形,子类调用父类的同步方法
* @author mashibing
*/

public class T {    synchronized void m() {        System.out.println("m start");        try {            TimeUnit.SECONDS.sleep(1);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println("m end");    }    public static void main(String[] args) {        new TT().m();//子类对象,new 子类的时候,先new了父类    }}class TT extends T {    @Override    synchronized void m() {        System.out.println("child m start");        super.m();// 也是this对象        System.out.println("child m end");    }}

/**
* 程序在执行过程中,如果出现异常,默认情况锁会被释放
* 所以,在并发处理的过程中,有异常要多加小心,不然可能会发生不一致的情况。
* 比如,在一个web app处理过程中,多个servlet线程共同访问同一个资源,这时如果异常处理不合适,
* 在第一个线程中抛出异常,其他线程就会进入同步代码区,有可能会访问到异常产生时的数据。
* 因此要非常小心的处理同步业务逻辑中的异常,一般数据回滚。
* @author mashibing
*/

public class T {    int count = 0;    synchronized void m() {        System.out.println(Thread.currentThread().getName() + " start");        while(true) {            count ++;            System.out.println(Thread.currentThread().getName() + " count = " + count);            try {                TimeUnit.SECONDS.sleep(1);            } catch (InterruptedException e) {                e.printStackTrace();            }            if(count == 5) {                int i = 1/0; //此处抛出异常,锁将被释放,要想不被释放,可以在这里进行catch,然后让循环继续            }        }    }    public static void main(String[] args) {        T t = new T();        Runnable r = new Runnable() {            @Override            public void run() {                t.m();            }        };        new Thread(r, "t1").start();        try {            TimeUnit.SECONDS.sleep(3);        } catch (InterruptedException e) {            e.printStackTrace();        }        //如果不抛出异常,t2永远不能运行到        new Thread(r, "t2").start();    }}
原创粉丝点击