标签:obj 区别 null 模式 好处 tin cal 表示 run
单例设计模式:保证类在内存中只有一个对象。
//饿汉式
package com.hwh.thread;
public class Demo1_Singleton {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2);
}
static class Singleton {
//1,私有构造方法,其他类不能访问该构造方法了
private Singleton(){}
//2,创建本类对象
private static Singleton s = new Singleton();
//3,对外提供公共的访问方法
public static Singleton getInstance() { //获取实例
return s;
}
}
}
//懒汉式, 单例的延迟加载模式
package com.hwh.thread;
public class Demo1_Singleton {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2);
}
static 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;
}
}
}
运行结果为true
package com.hwh.thread;
public class Demo1_Singleton {
public static void main(String[] args) {
Singleton s1 = Singleton.s; //成员变量被私有,不能通过类名.调用
Singleton s2 = Singleton.s;
System.out.println(s1 == s2);
}
static class Singleton {
//1,私有构造方法,其他类不能访问该构造方法了
private Singleton(){}
//2,声明一个引用
public static final Singleton s = new Singleton();
}
}
运行结果为true
package com.hwh.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");
}
}
package com.hwh.thread;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Demo3_Timer {
public static void main(String[] args) throws InterruptedException {
Timer t = new Timer();
//在指定时间安排指定任务
//第一个参数,是安排的任务,第二个参数是执行的时间,第三个参数是过多长时间再重复执行
t.schedule(new MyTimerTask(), new Date(120, 1, 21, 11, 26, 0),3000);
while(true) {
Thread.sleep(1000);
System.out.println(new Date());
}
}
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("起床背英语单词");
}
}
运行结果为
package com.hwh.thread2;
public class Demo1_Notify {
/**
* @param args
* 等待唤醒机制
*/
public static void main(String[] args) {
final Printer p = new Printer();
new Thread() {
public void run() {
while(true) {
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print2();
} catch (InterruptedException e) {
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("\r\n");
flag = 1;
this.notify();
}
}
}
运行结果为
我是韩文浩
欢迎光临
我是韩文浩
欢迎光临
.....
package com.hwh.thread2;
public class Demo2_NotifyAll {
public static void main(String[] args) {
final Printer2 p = new Printer2();
new Thread() {
public void run() {
while(true) {
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
/*1,在同步代码块中,用哪个对象(this)锁,就用哪个对象调用wait方法
* 2,为什么wait方法和notify方法定义在Object这类中?
* 因为锁对象可以是任意对象,Object是所有的类的基类,所以wait方法和notify方法需要定义在Object这个类中
* 3,sleep方法和wait方法的区别?
* a,sleep方法必须传入参数,参数就是时间,时间到了自动醒来
* wait方法可以传入参数也可以不传入参数,传入参数就是在参数的时间结束后等待,不传入参数就是直接等待
* b,sleep方法在同步函数或同步代码块中,不释放锁,睡着了也抱着锁睡
* wait方法在同步函数或者同步代码块中,释放锁
*/
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.notify(); //随机唤醒单个等待的线程
this.notifyAll();
}
}
public void print2() throws InterruptedException {
synchronized(this) {
while(flag != 2) {
this.wait(); //线程2在此等待
}
System.out.print("欢");
System.out.print("迎");
System.out.print("光");
System.out.print("临");
System.out.print("\r\n");
flag = 3;
//this.notify();
this.notifyAll();
}
}
public void print3() throws InterruptedException {
synchronized(this) {
while(flag != 3) {
this.wait(); //线程3在此等待,if语句是在哪里等待,就在哪里起来
//while循环是循环判断,每次都会判断标记
}
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.notify();
this.notifyAll();
}
}
}
package com.hwh.thread2;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Demo3_ReentrantLock {
public static void main(String[] args) {
final Printer3 p = new Printer3();
new Thread() {
public void run() {
while(true) {
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Printer3 {
private ReentrantLock r = new ReentrantLock();
private Condition c1 = r.newCondition();
private Condition c2 = r.newCondition();
private Condition c3 = r.newCondition();
private int flag = 1;
public void print1() throws InterruptedException {
r.lock(); //获取锁
if(flag != 1) {
c1.await();
}
System.out.print("我");
System.out.print("是");
System.out.print("韩");
System.out.print("文");
System.out.print("浩");
System.out.print("\r\n");
flag = 2;
//this.notify(); //随机唤醒单个等待的线程
c2.signal();
r.unlock(); //释放锁
}
public void print2() throws InterruptedException {
r.lock();
if(flag != 2) {
c2.await();
}
System.out.print("欢");
System.out.print("迎");
System.out.print("光");
System.out.print("临");
System.out.print("\r\n");
flag = 3;
//this.notify();
c3.signal();
r.unlock();
}
public void print3() throws InterruptedException {
r.lock();
if(flag != 3) {
c3.await();
}
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;
c1.signal();
r.unlock();
}
}
package com.hwh.thread2;
public class Demo4_ThreadGroup {
public static void main(String[] args) {
//demo1();
ThreadGroup tg = new ThreadGroup("我是一个新的线程组"); //创建新的线程组
MyRunnable mr = new MyRunnable(); //创建Runnable的子类对象
Thread t1 = new Thread(tg, mr, "张三"); //将线程t1放在组中
Thread t2 = new Thread(tg, mr, "李四"); //将线程t2放在组中
System.out.println(t1.getThreadGroup().getName()); //获取组名
System.out.println(t2.getThreadGroup().getName());//我是一个新的线程组 我是一个新的线程组
tg.setDaemon(true);
}
public static void demo1() {
MyRunnable mr = new MyRunnable();
Thread t1 = new Thread(mr, "张三");
Thread t2 = new Thread(mr, "李四");
ThreadGroup tg1 = t1.getThreadGroup();
ThreadGroup tg2 = t2.getThreadGroup();
System.out.println(tg1.getName()); //默认的是主线程
System.out.println(tg2.getName());//main main
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + "...." + i);
}
}
}
package com.hwh.thread2;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Demo5_Executors {
/**
* public static ExecutorService newFixedThreadPool(int nThreads)
* public static ExecutorService newSingleThreadExecutor()
*/
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);//创建线程池
pool.submit(new MyRunnable()); //将线程放进池子里并执行
pool.submit(new MyRunnable());
pool.shutdown(); //关闭线程池
}
}
运行结果为
pool-1-thread-1....0
pool-1-thread-2....0
pool-1-thread-1....1
pool-1-thread-1....2
pool-1-thread-1....3
pool-1-thread-2....1
pool-1-thread-1....4
pool-1-thread-2....2
pool-1-thread-1....5
pool-1-thread-2....3
.......
package com.hwh.thread2;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Demo6_Callable {
/**
* @param args
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(2);//创建线程池
Future<Integer> f1 = pool.submit(new MyCallable(100)); //将线程放进池子里并执行
Future<Integer> f2 = pool.submit(new MyCallable(50));
System.out.println(f1.get());
System.out.println(f2.get());
pool.shutdown(); //关闭线程池
}
}
class MyCallable implements Callable<Integer> {
private int num;
public MyCallable(int num) {
this.num = num;
}
@Override
public Integer call() throws Exception {
int sum = 0;
for(int i = 1; i <= num; i++) {
sum += i;
}
return sum;
}
}
运行结果为
5050
1275
标签:obj 区别 null 模式 好处 tin cal 表示 run
原文地址:https://www.cnblogs.com/albieh/p/12342228.html