Java foundation _ deadlock, thread group, timers Timer

 

 

First, the deadlock:

  Deadlock is a situation: more than one thread is blocked, one of them or all are waiting for a resource to be released. Since the thread is blocked indefinitely, so the program can not properly terminated.

 

  For example, the first thread is a need, when the lock is in an idle state, a given thread, the second thread of the two required, the second lock is also in an idle state, to two threads, this is no problem.

  However, when the first thread is a need to put the thread to the second two need not returned, they need to thread a second lock, then thread a thread will have to wait two lock to return, you may still need to thread two thread a lock, also in a wait state, therefore, a thread and thread loop waiting two remain, both threads are unable to return the lock to get things done, both deadlock situation. . .

  

 

package com.Gary1;

public class DeadLock {

    public static Object lock1 = new Object();
    public static Object lock2 = new Object();
    
    
    public static void main(String[] args) {
        
        new Thread(new Thread1()).start();
        new Thread(new Thread2()).start();
        
    }
    
}

class Thread1 implements Runnable{

    @Override
    public void run() {
        synchronized(DeadLock.lock1) { 
            System.out.println ( "to do after the first lock made things" );
             the try {
                 // takes 100 milliseconds, continue to do something 
                Thread.sleep (one hundred ); 
            } the catch (InterruptedException E) { 
                e.printStackTrace (); 
            } 
            the synchronized (DeadLock.lock2) { 
                System.out.println ( "do the following things Thread1 while achieving two locks" ); 
            } 
        } 
        
    } 
} 

class Thread2 the implements the Runnable { 

    @Override 
    public  voidRUN () {
         the synchronized (DeadLock.lock2) { 
            System.out.println ( "to do after the second lock made things" );
             the try {
                 // takes 100 milliseconds, continue to do something 
                Thread.sleep (one hundred ) ; 
            } the catch (InterruptedException E) { 
                e.printStackTrace (); 
            } 
            the synchronized (DeadLock.lock1) { 
                System.out.println ( "do the following things Thread2 while achieving two locks" ); 
            } 
        } 
        
        
    } 
}
DeadLock.java

 

  The best way to avoid this situation: locked solve order

  Thread lock on both sides of the order lock1-> lock2

 

package com.Gary1;

public class DeadLock {

    public static Object lock1 = new Object();
    public static Object lock2 = new Object();
    
    
    public static void main(String[] args) {
        
        new Thread(new Thread1()).start();
        new Thread(new Thread2()).start();
        
    }
    
}

class Thread1 implements Runnable{

    @Override
    public void run() {
        synchronized(DeadLock.lock1) {
            System.out.println("取得第一把锁之后要做的事情");
            try {
                //耗时100毫秒,继续做一些事情
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized(DeadLock.lock2) {
                System.out.println("Thread1同时取得两把锁之后要做的事情");
            }
        }
        
    }
}

class Thread2 implements Runnable{

    @Override
    public void run() {
        synchronized(DeadLock.lock1) {
            System.out.println("取得第二把锁之后要做的事情");
            try {
                //耗时100毫秒,继续做一些事情
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized(DeadLock.lock2) {
                System.out.println("Thread2同时取得两把锁之后要做的事情");
            }
        }
        
        
    }
}
DeadLock.java

 

 

二、线程组ThreadGroup 默认处于同一个组里面

    使用线程组可以统一设置这个组内线程的一些东西。比如设置优先级,设置是否是守护线程

    ThreadGroup tg = new ThreadGroup("我们的线程组");
        
    Thread t1 = new Thread(tg,r);
    Thread t2 = new Thread(tg,r);
        
    //批量管理
    tg.interrupt();//中断里边所有线程
    tg.setDaemon(true);//设置守护线程
    tg.setMaxPriority(9);//设置线程组最大优先级

 

  

 

package com.Gary1;

public class ThreadGroupDemo {

    public static void main(String[] args) {
        MyRunnable r = new MyRunnable();
        
        ThreadGroup tg = new ThreadGroup("我们的线程组");
        
        Thread t1 = new Thread(tg,r);
        Thread t2 = new Thread(tg,r);
        
        //批量管理
        tg.interrupt();//中断里边所有线程
        tg.setDaemon(true);//设置守护线程
        tg.setMaxPriority(9);//设置线程组最大优先级
        
        //ThreadGroup tg = t1.getThreadGroup();
        //输出线程名字
        //System.out.println(tg.getName());
        //输出线程组名字
        //System.out.println(t2.getThreadGroup().getName());
        
        t1.start();
        t2.start();
        
    }
    
}
ThreadGroupDemo.java

 

package com.Gary1;

public class MyRunnable implements Runnable{

    private String data = "";
    
    @Override
    public void run() {
        for(int i=0;i<100;i++) {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+":"+i);
        }
        
    }

}
MyRunnable.java

 

 

三、定时器Timer

  作用:一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。 

  使用类:Timer和TimerTask

  常用方法:

    timer.schedule(TimerTask task, long delay)   

    timer.schedule(TimerTask task, long delay, long period) 

    timer.schedule(TimerTask task, Date time) 

    timer.cancel();

public static void main(String[] args) {
        //Timer TimerTask
        Timer t = new Timer();
        
        //定义一个定时器任务,2000毫秒开始执行
        //t.schedule(new MyTimerTask(), 2000);
        //定义一个定时器任务,2000毫秒开始执行,每个3000毫秒执行一次
        //t.schedule(new MyTimerTask(), 2000,3000);
        //在哪个时间开始执行这个任务
        //t.schedule(new MyTimerTask(), time);
        //终止定时器任务执行
        //timer.cancel();
    }

 

package com.Gary1;

import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
    public static void main(String[] args) {
        //Timer TimerTask
        Timer t = new Timer();
        
        //定义一个定时器任务,2000毫秒开始执行
        //t.schedule(new MyTimerTask(), 2000);
        //定义一个定时器任务,2000毫秒开始执行,每个3000毫秒执行一次
        //t.schedule(new MyTimerTask(), 2000,3000);
        //在哪个时间开始执行这个任务
        //t.schedule(new MyTimerTask(), time);
        //终止定时器任务执行
        //timer.cancel();
    }

}

class MyTimerTask extends TimerTask{

    @Override
    public void run() {
        System.out.println("定时器任务");
        
    }
    
}
TimerDemo.java

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/1138720556Gary/p/11946543.html