单例设计模式:保证类在内存中只有一个对象。
如何保证类在内存中只有一个对象呢?
//饿汉式
class Singleton {
//1、私有构造函数
private Singleton() {}
//2、创建本类对象
private static Singleton singleton = new Singleton();
//3、对外提供公共的访问方法
public static Singleton getInstance() { //获取实例
return singleton;
}
}
//懒汉式
class Singleton1 {
//1、私有构造函数
private Singleton1() {
}
//2、声明一个本类的引用
private static Singleton1 singleton1;
//3、对外提供公共的访问方法
public static Singleton1 getInstance() { //获取实例
if (singleton1 == null) {
singleton1 = new Singleton1();
}
return singleton1;
}
}
public static void main(String[] args) throws IOException {
Runtime runtime = Runtime.getRuntime(); //获取运行时对象
//exec在单独的进程中执行指定的字符串命令
//runtime.exec("shutdown -s -t 300"); //300秒后关机
runtime.exec("shutdown -a"); //取消关机
}
线程用其安排以后在后台线程中执行的任务,可安排任务执行一次或者定期重复执行
public static void main(String[] args) throws IOException, InterruptedException {
Timer timer = new Timer();
//在指定时间安排指定任务
//第一个参数,是安排的任务,第二个参数是执行的时间(执行时间需要当前年份减去1900,月份范围0-11),第三个参数是重复执行的间隔时间
timer.schedule(new MyTimerTask(),new Date(123,7,22,13,49,30));
while (true){
Thread.sleep(1000);
System.out.println(new Date());
}
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("起床");
}
}
public static void main(String[] args) throws IOException, InterruptedException {
Timer timer = new Timer();
//在指定时间安排指定任务
//第一个参数,是安排的任务,第二个参数是执行的时间(执行时间需要当前年份减去1900,月份范围0-11),第三个参数是重复执行的间隔时间
//下面是到了指定时间后每5秒执行一次
timer.schedule(new MyTimerTask(),new Date(123,7,22,13,49,30),5000);
while (true){
Thread.sleep(1000);
System.out.println(new Date());
}
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("起床");
}
}
public static void main(String[] agr) {
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();
}
/**
* 非静态的同步方法的锁对象是this
* 静态的同步方法的锁对象是该类的字节码对象
*/
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(); //随机唤醒单个等待的线程
}
}
/*
* 非静态同步函数的锁是:this
* 静态的同步函数的锁是:字节码对象
*/
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 ; //改变flag的值,让当前线程等待,唤醒其他等待的线程
this.notify(); //随机唤醒单个等待的线程
}
}
}
public class Synchronized {
public static void main(String[] agr) {
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();
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();
}
}
/**
* 非静态的同步方法的锁对象是this
* 静态的同步方法的锁对象是该类的字节码对象
*/
class printer {
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(); //随机唤醒单个等待的线程
}
}
/*
* 非静态同步函数的锁是:this
* 静态的同步函数的锁是:字节码对象
*/
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("\r\n");
flag = 3 ; //改变flag的值,让当前线程等待,唤醒其他等待的线程
this.notifyAll(); //随机唤醒单个等待的线程
}
}
public void print3() throws InterruptedException {
synchronized (this) {
while (flag != 3){ //while循环是循环判断,每次都会判断标记
this.wait(); //当前线程等待
}
System.out.print("A");
System.out.print("B");
System.out.print("C");
System.out.print("D");
System.out.print("E");
System.out.print("F");
System.out.print("\r\n");
flag = 1 ; //改变flag的值,让当前线程等待,唤醒其他等待的线程
this.notifyAll(); //随机唤醒单个等待的线程
}
}
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Synchronized {
public static void main(String[] agr) {
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();
new Thread() {
public void run() {
while (true) {
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while (true) {
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
/**
* 非静态的同步方法的锁对象是this
* 静态的同步方法的锁对象是该类的字节码对象
*/
class printer {
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;
c2.signal();
r.unlock();
}
/*
* 非静态同步函数的锁是:this
* 静态的同步函数的锁是:字节码对象
*/
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; //改变flag的值,让当前线程等待,唤醒其他等待的线程
c3.signal();
r.unlock();
}
public void print3() throws InterruptedException {
r.lock();
if (flag != 3) {
c3.await(); //当前线程等待
}
System.out.print("A");
System.out.print("B");
System.out.print("C");
System.out.print("D");
System.out.print("E");
System.out.print("F");
System.out.print("\r\n");
flag = 1; //改变flag的值,让当前线程等待,唤醒其他等待的线程
c1.signal();
r.unlock();
}
}
A:线程组概述
public class ThreadGroup {
public static void main(String[] agr) {
//demo1();
java.lang.ThreadGroup tg = new java.lang.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);
}
private static void demo1() {
MyRunnable myRunnable = new MyRunnable();
Thread t1 = new Thread(myRunnable, "线程1");
Thread t2 = new Thread(myRunnable, "线程2");
java.lang.ThreadGroup tg1 = t1.getThreadGroup();
java.lang.ThreadGroup tg2 = t2.getThreadGroup();
System.out.println(tg1.getName()); //默认是主程序
System.out.println(tg2.getName());
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + "....." + i);
}
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Demo5_Executors {
public static void main(String[] agr) {
ExecutorService pool = Executors.newFixedThreadPool(2); //创建线程池
pool.submit(new MyRunnable()); //将线程放进线程池并执行
pool.submit(new MyRunnable());
pool.shutdown(); //关闭线程池
}
}