package Demo02;
//静态代理模式总结:
//1、真实对象(You)和代理对象(WeddingCompany)都要实现同一个接口。
//2、代理对象要代理真实对象(即,代理对象要有一个参数,把真实对象传进去)
//好处:
//1、代理对象可以做真实对象做不了的事情
//2、真实对象就可以专注做自己的事情;
//线程的Runnable创建方式就是:线程的代理模式。因为Thread和丢进Thread的Runnable接口实现类,都实现Runnable接口。
public class StaticProxy {
public static void main(String[] args) {
//真实对象,需要传入代理对象
You you = new You();
//代理对象
WeddingCompany weddingCompanynew = new WeddingCompany(you);
weddingCompanynew.HappyMarry();
//线程的Runnable创建方式就是:线程的代理模式。因为Thread和丢进Thread的Runnable接口实现类,都实现Runnable接口。
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("我爱你");
}
}).start();
}
}
//真实对象 和 代理对象 都要实现的同一个接口;
interface Marry{
void HappyMarry();
}
//真实对象---You;
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("秦老师要结婚了,超开心");
}
}
//代理对象----WeddingCompany婚庆公司
class WeddingCompany implements Marry{
//代理对象首先要有个被代理的对象;
private Marry target;
// 代理对象要代理真实对象,所以要是用构造方法传入真实对象
public WeddingCompany(Marry target) {
this.target = target;
}
@Override
//代理对象的重写方法里,即有真实对象做的事,也有真实对象做不了的事before(),after();
public void HappyMarry() {
before();
this.target.HappyMarry();
after();
}
private void before(){
System.out.println("结婚之前,布置现场");
}
private void after(){
System.out.println("结婚之后,收尾款");
}
}
下面是五个状态的详解:
package Demo03;
public class TestSleep {
public static void main(String[] args) {
try {
tenDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void tenDown() throws InterruptedException{
int num = 10;
while(true){
Thread.sleep(1000);//运行的线程延时1s
System.out.println(num--);
if(num<=0) break;
}
}
}
package Demo03;
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield, "a").start();
new Thread(myYield, "b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "线程开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName() + "线程停止执行");
}
}
package Demo03;
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("/");
}
});
//观察状态;
Thread.State state = thread.getState();
System.out.println(state);//NEW. 因为还未条用start开始线程,所以状态为NEW
//观察启动后状态
thread.start();
state = thread.getState();//更新线程状态。即,获取现在线程的状态。
System.out.println(state);//RUNNABLE
while(state != Thread.State.TERMINATED){
//判断线程是否死亡,不死亡的情况下,输出线程启动后的状态
Thread.sleep(100);//TIMED_WAITING
state = thread.getState();//更新线程状态;
System.out.println(state);
}
}
}
性能倒置
。thread.setDaemon(true);
同时
买票,票数是不够的,这时票数会变成-2张,这就是并发
问题。所以需要线程同步
,即一个人买后,票数同步更新,下一个人再买,发现票数不够,就不能买了。这就需要队列+锁
。简单来说:线程同步,同步的是被修改的部分,使修改实时同步。
队列+锁
比如一群人排队上厕所,只排好队一个一个的进厕所,会出现厕所里人越来越多,导致厕所没那么多空间储存那么多人。一个好的方法是在厕所门上装一个锁,进去一个人,就上锁,等这个人上完厕所,再开锁,让下一个人进。这样就可以解决空间问题。
package Demo03;
public class UnsafeBank {
public static void main(String[] args) {
Account account = new Account(100, "结婚基金");
Drawing you = new Drawing(account, 50, "你");
Drawing girlFriend = new Drawing(account, 100, "girlFriend");
you.start();
girlFriend.start();
}
}
//账户
class Account{
int money;//余额
String name;//卡名
public Account(int money, String name) {
this.money = money;
this.name = name;
}
}
//银行:模拟取款; Drawing:取钱
class Drawing extends Thread{
//取钱首先要有个账户
Account account;
//取了多少钱
int drawingMoney;
//还剩多少钱
int nowMoney;
public Drawing(Account account, int drawingMoney, String name){
super(name);
this.account = account;
this.drawingMoney = drawingMoney;
}
@Override
public void run() {
//首先判断有没有钱
if(account.money - drawingMoney <0){
System.out.println(Thread.currentThread().getName() + "钱不够了,取不了");
return;
}
//利用延时放大问题的发生性
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//卡内余额 = 余额 - 取得钱
account.money = account.money - drawingMoney;
//手里的钱
nowMoney = nowMoney + drawingMoney;
System.out.println(account.name + "余额为:" + account.money);
//Thread.currentThread().getName() = this.getName()
System.out.println(this.getName() + "手里的钱:" + nowMoney);
}
}
@Override
public void run() {
synchronized (account){
//首先判断有没有钱
if(account.money - drawingMoney <0){
System.out.println(Thread.currentThread().getName() + "钱不够了,取不了");
return;
}
//利用延时放大问题的发生性
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//卡内余额 = 余额 - 取得钱
account.money = account.money - drawingMoney;
//手里的钱
nowMoney = nowMoney + drawingMoney;
System.out.println(account.name + "余额为:" + account.money);
//Thread.currentThread().getName() = this.getName()
System.out.println(this.getName() + "手里的钱:" + nowMoney);
}
}
}
导致程序卡死
。只有双方都放开自己手里的东西,死锁才会解决。死锁的代码:原因是
synchronized同步块同时包含两个锁
package Demo03;
//死锁:多个线程互相抱着对方的资源(互相抱着对方需要的锁),然后形成僵持
public class DeadLock {
public static void main(String[] args) {
Makeup girl1 = new Makeup(0, "灰姑凉");
Makeup girl2 = new Makeup(1, "白雪公主");
girl1.start();
girl2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
//线程做的是化妆(makeup)这件事
class Makeup extends Thread{
//需要的资源只有一份(即,只有一个口红和一个镜子),用static来保证只有一份;
//限定一份是为了模拟死锁
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;//选择哪个人化妆
String girlName;//使用化妆品的人
public Makeup(int choice, String girlName){
this.choice = choice;
this.girlName = girlName;
}
@Override
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException {
if(choice == 0){
synchronized (lipstick){
//同步块获得lipstick的锁
System.out.println(this.girlName + "获得口红的锁");
Thread.sleep(1000);
synchronized (mirror){
//一秒钟后,同步块获得mirror的锁
System.out.println(this.girlName + "获得镜子的锁");
}
}
}else{
synchronized (mirror){
//同步块获得mirror的锁
System.out.println(this.girlName + "获得镜子的锁");
Thread.sleep(1000);
synchronized (lipstick){
//一秒钟后,同步块获得lipstick的锁
System.out.println(this.girlName + "获得口红的锁");
}
}
}
}
}
解决办法:把两个锁中的一个,放在同步块外面,不让一个同步块同时包含两个锁就行。
private void makeup() throws InterruptedException {
if(choice == 0){
synchronized (lipstick){
//同步块获得lipstick的锁
System.out.println(this.girlName + "获得口红的锁");
Thread.sleep(1000);
}
synchronized (mirror){
//一秒钟后,同步块获得mirror的锁
System.out.println(this.girlName + "获得镜子的锁");
}
}else{
synchronized (mirror){
//同步块获得mirror的锁
System.out.println(this.girlName + "获得镜子的锁");
Thread.sleep(1000);
}
synchronized (lipstick){
//一秒钟后,同步块获得lipstick的锁
System.out.println(this.girlName + "获得口红的锁");
}
}
}
例子:买票
package Demo03;
import java.util.concurrent.locks.ReentrantLock;
public class TestLock {
public static void main(String[] args) {
TestLock2 testLock2 = new TestLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class TestLock2 implements Runnable{
int ticketNums = 10;
//定义锁
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true){
//lock.lock();//加锁,或放这
try{
//票数ticketNums变化,所以在这加锁
lock.lock();//加锁
if(ticketNums>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNums--);
}else break;
}finally {
lock.unlock();//解锁
}
}
}
}