一,礼让和守护线程
package com.much.hard;public class TestYieldProtect {public static void main(String[] args) {Yield1 y1 = new Yield1();y1.setName("A");Yield2 y2 = new Yield2();y2.setName("B");//y1.start();//y2.start();Daemon1 d1 = new Daemon1();d1.setName("boss");Daemon2 d2 = new Daemon2();d2.setName("bodygGuard");d2.setDaemon(true);d1.start();d2.start();// d2 本来要打印500次的,d1打印10次,可它是守护线程,老板挂了,它也挂了。// 守护线程的终止是自身无法控制的,因此千万不要把IO、File等重要操作逻辑分配给它;因为它不靠谱;// GC垃圾回收线程就是很好的例子。/* 当我们的程序中不再有任何运行的Thread,* 程序就不会再产生垃圾,垃圾回收器也就无事可做,* 所以当垃圾回收线程是JVM上仅剩的线程时,垃圾回收线程会自动离开。* 它始终在低级别的状态中运行,用于实时监控和管理系统中的可回收资源。*/}}class Yield1 extends Thread {public void run() {for (int i = 0; i < 100; i++) {// 暂停当前正在执行的线程对象,并执行其他线程。让多线程执行更和谐,而不是一人一次。if (i == 9) yield();System.out.println(getName() + "\t" + i);}}
}class Yield2 extends Thread {public void run() {for (int i = 0; i < 100; i++) {System.out.println(getName() + "\t" + i);}}
}class Daemon1 extends Thread {public void run() {for (int i = 0; i < 10; i++) {System.out.println(getName() + "\t" + i);}}
}class Daemon2 extends Thread {public void run() {for (int i = 0; i < 500; i++) {System.out.println(getName() + "\t" + i);}}
}
礼让 当A线程是9时,让B线程执行。
二,加入线程
package com.much.hard;public class TestJoin {public static void main(String[] args) {System.out.println("main线程开启啦");Thread1 t1 = new Thread1();Thread2 t2 = new Thread2();t1.start();t2.start();// 让1,2线程加入main线程前面 两个线程开启... cpu分配交互执行。// t1,t2可用来计算,把结果反馈到main里面。try {t1.join();t2.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("main线程结束啦");}public static void method() {}}class Thread1 extends Thread {public void run() {for (int i = 0; i < 100; i++) {System.out.println(getName() +": " + i);}}
}class Thread2 extends Thread {public void run() {for (int i = 0; i < 100; i++) {System.out.println(getName() +": " + i);}}
}
三,锁
有了锁,会让一个线程结束后,执行下一个线程。。。
package com.much.hard;public class TestSynchronize {public static void main(String[] args) {// 锁对同一个对象有用 this锁(同步代码块)Print p = new Print();ThreadShow1 ts1 = new ThreadShow1(p);ts1.start();ThreadShow2 ts2 = new ThreadShow2(p);ts2.start();/*Timer t = new Timer();Thread test = new Thread(t);test.start();*/// 里面new对象,和同传入同一个对象的区别。要同一个对象作为锁。// 问题1, abcd 1234 间隔的出现? cpu的资源随机分配。// 问题2,用锁解决,锁住方法,等这个方法(线程)执行完,再执行下一个线程。// 问题3,别人讲,可以来卖票。。。计数。。。}}class Print {public synchronized void printNum() {// 普通同步方法默认的锁时 this关键字System.out.print(1);System.out.print(2);System.out.print(3);System.out.println(4);}public void printLetter() {synchronized(this) {System.out.print("a");System.out.print("b");System.out.print("c");System.out.println("d");}}
}class ThreadShow1 extends Thread {// 第一个线程里面打印100条数字。Print p = null;public ThreadShow1() {}public ThreadShow1(Print p) {this.p = p;}public void run() {//Print p = new Print();for (int i = 0; i < 100; i++) {p.printNum();System.out.println(getName());}}
}class ThreadShow2 extends Thread {Print p = null;public ThreadShow2() {}public ThreadShow2(Print p) {this.p = p;}// 第二个线程打印100条字母。public void run() {//Print p = new Print();for (int i = 0; i < 100; i++) {p.printLetter();System.out.println(getName());}}
}// 实现Runnable接口。class Timer implements Runnable{public void run() {System.out.println("10秒计时,马上爆炸...");for (int i = 1; i <= 10; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(i);} }
}
四,饿汉式单例
锁机制的应用。
package com.much.hard;public class TestSingle {public static void main(String[] args) {//二:编写多线程单例模式(懒汉式)//饿汉式,就不用考虑啥了,直接返回,不多bb。for (int i = 0; i < 20; i++) {my1 m = new my1();m.start();}}}class Dog {private static Dog dog = null;private Dog() {}public static Dog show() {if (dog == null) { // 锁 一起执行。 synchronized(Dog.class) {if (dog == null) {// 线程休眠0.1秒。后再执行。try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}dog = new Dog();}} }return dog;}
}class my1 extends Thread {public void run() {System.out.println(Dog.show()); }
}
五,等待处理机制
package com.much.hard;public class TestWaiting {public static void main(String[] args) {// 等待唤醒机制。// 需求,我想用线程实现 1 2 1 2 1 2 1 2... 的循环。PrintTwo pt = new PrintTwo();Wait1 w1 = new Wait1(pt);Wait2 w2 = new Wait2(pt);w1.start();w2.start();}}class PrintTwo {int i = 1;public synchronized void print1() {if (i != 1) {try {this .wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(1);i = 2;// 唤醒下个线程。// notify是唤醒等待中的一个(随机的)获取对象锁,进入可运行状态,this.notify();}public synchronized void print2() {if (i == 1) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();} }System.out.println(2);i = 1;this.notify(); }
}class Wait1 extends Thread {PrintTwo pt = null;public Wait1() {}public Wait1(PrintTwo pt) {this.pt = pt;}public void run() {while (true)pt.print1();}
}class Wait2 extends Thread {PrintTwo pt = null;public Wait2() {}public Wait2(PrintTwo pt) {this.pt = pt;}public void run() {while (true)pt.print2();}
}
哈哈哈,初学线程,有点点懵,一下子太多了,我只是把老师讲的小demo写了一遍,应用还没有基本。再此做下笔记。