并发编程学习笔记
1.线程安全:当多个线程访问某一个类(对象或方法)时,这个类始终能表现出正确的行为,那么这个类(对象或方法)就是线程安全的
2.对象锁和类锁
具体实例,看看什么是对象锁,什么是类锁:
对象锁:
package com.zhangshitong.thread01; public class MyThread extends Thread{ private int count=5; //synchronized 加锁 public synchronized void run(){ count--; System.out.println(this.currentThread().getName() + "count ="+ count); } public static void main(String[] args) { /** * 分析:当多个线程访问myThread的run方法时,以排队的方式进行处理(这里排队是按cpu分配的先后顺序而定的) * */ MyThread myThread =new MyThread(); Thread t1=new Thread(myThread,"t1"); Thread t2=new Thread(myThread,"t2"); Thread t3=new Thread(myThread,"t3"); Thread t4=new Thread(myThread,"t4"); Thread t5=new Thread(myThread,"t5"); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); } }
以上就是对象锁,下面看一下类锁:
package com.zhangshitong.thread01; public class MultiThread { private int num=0; /** * static */ public synchronized void printNum(String tag){ try { if(tag.equals("a")){ num=100; System.out.println("tag a, set num over!"); Thread.sleep(1000); }else{ num=200; System.out.println("tag b, set num over!"); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void main(String[] args) { //两个不同对象 final MultiThread m1=new MultiThread(); final MultiThread m2=new MultiThread(); Thread t1=new Thread(new Runnable() { @Override public void run() { m1.printNum("a"); } }); Thread t2=new Thread(new Runnable() { @Override public void run() { m1.printNum("b"); } }); } }
这个可以看出,是两个对象,那么锁也就是两个锁了,所以t1和t2之间不存在竞争,那如果想让t1和t2之间出现京城,该怎么做呢?
就是把属性num前加static 在printNum方法前加static 解释如下:在静态方法上加上synchronized,那么线程所获得的锁就是这个类级别的锁了
2.
举例如下:
package com.bjsxt.base.sync005; /** * synchronized的重入 * @author alienware * */ public class SyncDubbo1 { public synchronized void method1(){ System.out.println("method1.."); method2(); } public synchronized void method2(){ System.out.println("method2.."); method3(); } public synchronized void method3(){ System.out.println("method3.."); } public static void main(String[] args) { final SyncDubbo1 sd = new SyncDubbo1(); Thread t1 = new Thread(new Runnable() { @Override public void run() { sd.method1(); } }); t1.start(); } }
3.存储过程,游标 for 加 begin end 中有 exception 记录出错的操作,并写入日志:
4.异常:interruptedexception continue 跑出本次异常,并截止往下执行,如果是RuntimeException异常,那么程序直接就停止了