26-JAVA中的多线程与GUI

###25.01_多线程(单例设计模式)(掌握)
* 单例设计模式:保证类在内存中只有一个对象。

* 如何保证类在内存中只有一个对象呢?
    * (1)控制类的创建,不让其他类来创建本类的对象。private
    * (2)在本类中定义一个本类的对象。Singleton s;
    * (3)提供公共的访问方式。  public static Singleton getInstance(){return s}
* 单例写法两种:
    * (1)饿汉式 开发用这种方式。
    * 
            //饿汉式
            class Singleton {
                //1,私有构造函数
                private Singleton(){}
                //2,创建本类对象
                private static Singleton s = new Singleton();
                //3,对外提供公共的访问方法
                public static Singleton getInstance() {
                    return s;
                }
                
                public static void print() {
                    System.out.println("11111111111");
                }
            }
    * (2)懒汉式 面试写这种方式。多线程的问题?
    * 
            //懒汉式,单例的延迟加载模式
            class Singleton {
                //1,私有构造函数
                private Singleton(){}
                //2,声明一个本类的引用
                private static Singleton s;
                //3,对外提供公共的访问方法
                public static Singleton getInstance() {
                    if(s == null)
                        //线程1,线程2
                        s = new Singleton();
                    return s;
                }
                
                public static void print() {
                    System.out.println("11111111111");
                }
            }
    * (3)第三种格式
    * 
            class Singleton {
                private Singleton() {}
            
                public static final Singleton s = new Singleton();//final是最终的意思,被final修饰的变量不可以被更改
            }

package com.heima.thread;

public class Demo1_Singleton {

	/**
	 * @param args
	 * * 单例设计模式:保证类在内存中只有一个对象。
	 */
	public static void main(String[] args) {
		//Singleton s1 = new Singleton();
		
		Singleton s1 = Singleton.s;				//成员变量被私有,不能通过类名.调用
		//Singleton.s = null;
		Singleton s2 = Singleton.s;
		
		System.out.println(s1 == s2);
		
	/*	Singleton s1 = Singleton.getInstance();
		Singleton s2 = Singleton.getInstance();
		
		System.out.println(s1 == s2);*/
	}

}

/*
 * 饿汉式
 
class Singleton {
	//1,私有构造方法,其他类不能访问该构造方法了
	private Singleton(){}
	//2,创建本类对象
	private static Singleton s = new Singleton();
	//3,对外提供公共的访问方法
	public static Singleton getInstance() {				//获取实例
		return s;
	}
}*/
/*
 * 懒汉式,单例的延迟加载模式
 */
/*class Singleton {
	//1,私有构造方法,其他类不能访问该构造方法了
	private Singleton(){}
	//2,声明一个引用
	private static Singleton s ;
	//3,对外提供公共的访问方法
	public static Singleton getInstance() {				//获取实例
		if(s == null) {
			//线程1等待,线程2等待
			s = new Singleton();
		}
		
		return s;
	}
}*/
/*
 * 饿汉式和懒汉式的区别
 * 1,饿汉式是空间换时间,懒汉式是时间换空间
 * 2,在多线程访问时,饿汉式不会创建多个对象,而懒汉式有可能会创建多个对象
 */

class Singleton {
	//1,私有构造方法,其他类不能访问该构造方法了
	private Singleton(){}
	//2,声明一个引用
	public static final Singleton s = new Singleton();
	
}


###25.02_多线程(Runtime类)
* Runtime类是一个单例类
    * 
            Runtime r = Runtime.getRuntime();
            //r.exec("shutdown -s -t 300");        //300秒后关机
            r.exec("shutdown -a");                //取消关机

package com.heima.thread;

import java.io.IOException;

public class Demo2_Runtime {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		Runtime r = Runtime.getRuntime();			//获取运行时对象
		//r.exec("shutdown -s -t 300");
		r.exec("shutdown -a");
	}

}

###25.03_多线程(Timer)(掌握)
* Timer类:计时器

            public class Demo5_Timer {
                /**
                 * @param args
                 * 计时器
                 * @throws InterruptedException 
                 */
                public static void main(String[] args) throws InterruptedException {
                    Timer t = new Timer();
                    t.schedule(new MyTimerTask(), new Date(114,9,15,10,54,20),3000);
                    
                    while(true) {
                        System.out.println(new Date());
                        Thread.sleep(1000);
                    }
                }
            }
            class MyTimerTask extends TimerTask {
                @Override
                public void run() {
                    System.out.println("起床背英语单词");
                }
                
            }

package com.heima.thread;

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

public class Demo3_Timer {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		Timer t = new Timer();
		//在指定时间安排指定任务
		//第一个参数,是安排的任务,第二个参数是执行的时间,第三个参数是过多长时间再重复执行
		t.schedule(new MyTimerTask(), new Date(188, 6, 1, 14, 22, 50),3000);	
		
		while(true) {
			Thread.sleep(1000);
			System.out.println(new Date());
		}
	}

}

class MyTimerTask extends TimerTask {

	@Override
	public void run() {
		System.out.println("起床背英语单词");
	}
}

###25.04_多线程(两个线程间的通信)(掌握)
* 1.什么时候需要通信
    * 多个线程并发执行时, 在默认情况下CPU是随机切换线程的
    * 如果我们希望他们有规律的执行, 就可以使用通信, 例如每个线程执行一次打印
* 2.怎么通信
    

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值