sleep阻塞
线程提供了一个静态方法:
- public class SleepDemo {
-
- public static void main(String[] args) {
-
- System.out.println("程序开始了!");
-
- try {
-
- Thread.sleep(5000);//主线程阻塞5秒钟
-
- } catch (InterruptedException e) {
-
- e.printStackTrace();
-
- }
-
- System.out.println("程序结束了!");
-
- }
-
- }
sleep方法处理异常:InterruptedException.
当一个线程调用sleep方法处于睡眠阻塞的过程中,该线程的interrupt()方法被调用时,sleep方法会抛出该异常从而打断睡眠阻塞.
守护线程与普通线程的区别:守护线程是通过普通线程调用setDaemon(true)设置而来的
主要区别体现在当java进程中所有的普通线程都结束时进程会结束,在结束前会杀死所有还在运行的守护线程。
- public class DaemonThreadDemo {
- public static void main(String[] args) {
- Thread demo1 = new Thread(){
- public void run(){
- for(int i=0;i<5;i++){
- System.out.println("222");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- }
- System.out.println("111");
- }
- };
-
- Thread demo2 = new Thread(){
- public void run(){
- while(true){
- System.out.println("333");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- }
- }
- };
- demo1.start();
- demo2.setDaemon(true);//设置守护线程必须在线程启动前进行
- demo2.start();
-
- }
- }
重点:多线程并发安全问题
- public class SyncDemo {
- public static void main(String[] args) {
- Table table = new Table();
- Thread t1 = new Thread(){
- public void run(){
- while(true){
- int bean = table.getBean();
- Thread.yield();
- System.out.println(getName()+":"+bean);
- }
- }
- };
- Thread t2 = new Thread(){
- public void run(){
- while(true){
- int bean = table.getBean();
- /*
- static void yield()
- 线程提供的这个静态方法作用是让执行该方法的线程
- 主动放弃本次时间片。
- 这里使用它的目的是模拟执行到这里CPU没有时间了,发生
- 线程切换,来看并发安全问题的产生。
- */
- Thread.yield();
- System.out.println(getName()+":"+bean);
- }
- }
- };
- t1.start();
- t2.start();
- }
- }
-
- class Table{
- private int beans = 20;//桌子上有20个豆子
-
- public int getBean(){
- if(beans==0){
- throw new RuntimeException("没有豆子了!");
- }
- Thread.yield();
- return beans--;
- }
- }
synchronized的两种用法
- public class SyncDemo {
- public static void main(String[] args) {
- Table table = new Table();
- Thread t1 = new Thread(){
- public void run(){
- while(true){
- int bean = table.getBean();
- Thread.yield();
- System.out.println(getName()+":"+bean);
- }
- }
- };
- Thread t2 = new Thread(){
- public void run(){
- while(true){
- int bean = table.getBean();
- /*
- static void yield()
- 线程提供的这个静态方法作用是让执行该方法的线程
- 主动放弃本次时间片。
- 这里使用它的目的是模拟执行到这里CPU没有时间了,发生
- 线程切换,来看并发安全问题的产生。
- */
- Thread.yield();
- System.out.println(getName()+":"+bean);
- }
- }
- };
- t1.start();
- t2.start();
- }
- }
-
- class Table{
- private int beans = 20;//桌子上有20个豆子
-
- /**
- * 当一个方法使用synchronized修饰后,这个方法称为同步方法,多个线程不能
- * 同时执行该方法。
- * 将多个线程并发操作临界资源的过程改为同步操作就可以有效的解决多线程并发
- * 安全问题。
- * 相当于让多个线程从原来的抢着操作改为排队操作。
- */
- public synchronized int getBean(){
- if(beans==0){
- throw new RuntimeException("没有豆子了!");
- }
- Thread.yield();
- return beans--;
- }
- }
- public class SyncDemo4 {
- public static void main(String[] args) {
- Foo foo = new Foo();
- Thread t1 = new Thread(){
- public void run(){
- foo.methodA();
- }
- };
- Thread t2 = new Thread(){
- public void run(){
- foo.methodB();
- }
- };
- t1.start();
- t2.start();
- }
- }
- class Foo{
- public synchronized void methodA(){
- Thread t = Thread.currentThread();
- try {
- System.out.println(t.getName()+":正在执行A方法...");
- Thread.sleep(5000);
- System.out.println(t.getName()+":执行A方法完毕!");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public synchronized void methodB(){
- Thread t = Thread.currentThread();
- try {
- System.out.println(t.getName()+":正在执行B方法...");
- Thread.sleep(5000);
- System.out.println(t.getName()+":执行B方法完毕!");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }