一、异常
finally存在的意义,不管try有没有出现异常,或者catch有没有捕获异常,finally都会执行
二、线程
创建多线程程序的第一种方法(继承Thread类,重写run方法)
public class A02thread extends Thread { public static void main(String[] args) { A02thread a02thread = new A02thread("子线程");//构造方法设置线程名称 a02thread.start(); Thread.currentThread().setName("主线程");//设置线程名称 for (int i = 0; i < 10; i++) { try { Thread.sleep(1000);//线程睡眠 } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + i);//获取线程名称 } } public A02thread() { } public A02thread(String name) { super(name); } @Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(1000);//线程睡眠 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName() + ":" + i);//获取线程名称 } } }
创建多线程程序的第二种方法(实现Runable接口)
public class A02thread implements Runnable { public static void main(String[] args) { Runnable a02thread = new A02thread(); Thread thread = new Thread(a02thread); thread.start(); for (int i = 0; i < 10; i++) { System.out.println("主线程:" + i); } } @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("子线程:" + i); } } }
备注:推荐接口方式,继承只能有一个,实现可以有多个。
匿名内部类方式
public class A02thread { public static void main(String[] args) { new Thread() { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } } }.start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } } }).start(); } }
三、线程安全
同步代码块
public class A02thread implements Runnable { public static void main(String[] args) { A02thread a02thread = new A02thread(); new Thread(a02thread).start(); new Thread(a02thread).start(); new Thread(a02thread).start(); } private int resource = 10; private Object obj = new Object();//锁对象 @Override public void run() { while (true) { synchronized (obj) { if (resource > 0) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + resource); resource--; } } } } }
同步方法
public class A02thread implements Runnable { public static void main(String[] args) { A02thread a02thread = new A02thread(); new Thread(a02thread).start(); new Thread(a02thread).start(); new Thread(a02thread).start(); } private int resource = 10; @Override public void run() { while (true) { threadrunmethod(); } } public synchronized void threadrunmethod() {//锁对象为this,相当于synchronized (this){} if (resource > 0) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + resource); resource--; } } }
静态同步方法
public class A02thread implements Runnable { public static void main(String[] args) { A02thread a02thread = new A02thread(); new Thread(a02thread).start(); new Thread(a02thread).start(); new Thread(a02thread).start(); } private static int resource = 10; @Override public void run() { while (true) { threadrunmethod(); } } public static synchronized void threadrunmethod() {//锁对象为当前类的class,相当于synchronized (A02thread.class){} if (resource > 0) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + resource); resource--; } } }
四、线程状态(生产者与消费者例子)
public class practice { public static void main(String[] args) { Object obj = new Object(); //生产者 new Thread() { @Override public void run() { synchronized (obj) { try { obj.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("生产者开始造包子"); for (int i = 0; i < 3; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println((i + 1) + "秒"); } System.out.println("生产者造好包子"); obj.notify(); } } }.start(); //消费者 new Thread() { @Override public void run() { synchronized (obj) { System.out.println("消费者想要吃包子,等待生产者生产包子"); obj.notify(); try { obj.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("消费者开始吃包子"); } } }.start(); } }
五、线程池
public class Practice implements Runnable { public static void main(String[] args) { ExecutorService threadPool = Executors.newFixedThreadPool(2); threadPool.submit(new Practice()); threadPool.submit(new Practice()); threadPool.submit(new Practice()); threadPool.shutdown();//不建议使用 } @Override public void run() { System.out.println(Thread.currentThread().getName() + "执行线程任务"); } }