Runtime类是一个单例类。我们可以进行查看源码可以看见,Runtime类就是运用饿汉式的方法进行实现。 //可以执行一些字符串命令,就是字cmd中的一些命令
public static void main(String[] args) throws IOException { //获取运行时对象 Runtime runtime=Runtime.getRuntime(); //可以执行一些字符串命令,就是字cmd中的一些命令 //r.exec("shutdown -s -t 300"); //300秒后关机 runtime.exec("shutdown -a"); //取消关机 }应用场景:在指定时间安排我们执行什么事情。一种工具,线程用安排以后在后台线程中执行的任务,可安排任务执行一次,或者定期重复执行。比如闹钟。
public static void main(String[] args) throws InterruptedException { Timer timer=new Timer(); //在指定时间安排指定任务,第一个参数是安排的任务, //第二个参数是执行的时间,第三个参数是过多长时间再重复执行 timer.schedule(new MyTimerTask(), new Date(120,10,2,16,52,10),3000); while (true) { Thread.sleep(1000); System.out.println(new Date()); } } class MyTimerTask extends TimerTask{ @Override public void run() { // TODO Auto-generated method stub System.out.println("吃饭"); } }效果和倒计时一样,会在固定的时间。执行一次吃饭任务。
案例:我们想要将两个线程,进行两个方法隔行打印。所以我们需要一个等待唤醒机制
public static void main(String[] args) { final Printer printer =new Printer(); new Thread() { public void run() { while (true) { try { printer.print1(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); new Thread() { public void run() { while (true) { try { printer.print2(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); } class Printer{ private int flag = 1; public void print1() throws InterruptedException{ synchronized(this) { if (flag !=1) { // this.wait(); } System.out.print("我"); System.out.print("是"); System.out.print("小"); System.out.print("学"); System.out.print("生"); System.out.print("\r\n"); flag =2; this.notify(); //随机唤醒单个等待线程 } } public void print2() throws InterruptedException{ synchronized (this){ if (flag !=2) { this.wait(); } System.out.print("我"); System.out.print("是"); System.out.print("大"); System.out.print("学"); System.out.print("生"); System.out.print("\r\n"); flag =1; this.notify(); } } }效果如下: 解释:this.wait()表示该线程进行等待去执行其他线程。this.notify()表示该线程唤醒。我们设置一个flag 一开始它的值为1。那么当它不等于1的时候就等待。但是它等于1.所以就进行了:我是小学生的操作。随后我们改变了flag值。这时候cpu是随机分配线程的。当进入我是小学生这个线程是。flag 并不是1。进入if判断,从而导致该线程执行了wait()操作。
多个线程通信的问题
notify()方法是随机唤醒一个线程notifyAll()方法是唤醒所有线程JDK5之前无法唤醒指定的一个线程如果多个线程之间通信, 需要使用notifyAll()通知所有线程, 用while来反复判断条件 public static void main(String[] args) { final Printer2 printer2 =new Printer2(); new Thread() { public void run() { while (true) { try { printer2.print1(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); new Thread() { public void run() { while (true) { try { printer2.print2(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); new Thread() { public void run() { while (true) { try { printer2.print3(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); } class Printer2{ private int flag =1; public void print1() throws InterruptedException{ synchronized(this) { while (flag !=1) { // this.wait(); } System.out.print("我"); System.out.print("是"); System.out.print("小"); System.out.print("学"); System.out.print("生"); System.out.print("\r\n"); flag =2; this.notifyAll(); //随机唤醒单个等待线程 } } public void print2() throws InterruptedException{ synchronized (this){ while (flag !=2) { this.wait(); } System.out.print("我"); System.out.print("是"); System.out.print("大"); System.out.print("学"); System.out.print("生"); System.out.print("\r\n"); flag =3; this.notifyAll(); } } public void print3() throws InterruptedException{ synchronized (this){ while (flag !=3) { this.wait(); } System.out.print("大"); System.out.print("家"); System.out.print("都"); System.out.print("是"); System.out.print("小"); System.out.print("学"); System.out.print("生"); System.out.print("\r\n"); flag =1; this.notifyAll(); } } }TIPS:
在同步代码块中,用哪个对象锁,就用哪个对象调用wait方法
为什么wait方法和notify方法定义在Object这类中? 因为锁对象可以是任意对象,Object是所有的类的基类,所以wait方法和notify方法需要定义在Object这个类中
sleep方法和wait方法有什么区别? sleep方法必须传入参数,参数就是时间,时间到了自动醒来。wait方法可以传入参数,也可以不传入参数。传入参数就是在参数的时间结束后等待。不传入参数就直接等待。sleep方法在同步函数或同步代码块中,不释放锁。wait方法在同步函数或者同步代码块中,释放锁
线程组概述
Java中使用ThreadGroup来表示线程组,它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制。默认情况下,所有的线程都属于主线程组。public final ThreadGroup getThreadGroup()//通过线程对象获取他所属于的组public final String getName()//通过线程组对象获取他组的名字我们也可以给线程设置分组 1,ThreadGroup(String name) 创建线程组对象并给其赋值名字2,创建线程对象3,Thread(ThreadGroup?group, Runnable?target, String?name)4,设置整组的优先级或者守护线程 public static void main(String[] args) { // TODO Auto-generated method stub //自己设置线程组 ThreadGroup tg= new ThreadGroup("我是一个新的线程组"); //创建新的线程组 MyRunnable myRunnable =new MyRunnable(); //创建runnable的子类对象 Thread t1=new Thread(tg,myRunnable,"张三"); //将线程t1放在组中 Thread t2=new Thread(tg,myRunnable,"李四"); //将线程t2放在组中 System.out.println(t1.getThreadGroup().getName()); //获取组名 System.out.println(t2.getThreadGroup().getName()); tg.setDaemon(true); } MyRunnable myRunnable =new MyRunnable(); Thread t1 =new Thread(myRunnable,"张三"); Thread t2 =new Thread(myRunnable,"李四"); ThreadGroup tg1=t1.getThreadGroup(); //默认是主线程 ThreadGroup tg2=t2.getThreadGroup(); System.out.println(tg1.getName()); System.out.println(tg2.getName());