Ticket.java
package com.dym.jvm;
class Ticket //资源类
{
private int number = 30;
public synchronized void saleTicket() {
if (number > 0) {
System.out.println(Thread.currentThread().getName() + "\t 卖出第:" + (number--) + "\t 还剩下:" + number);
}
}
}
/*
* 题目:三个售票员 卖出 30张票
*
* 多线程编程的企业级套路+模板
*
* 在高内聚低耦合的前提下, 线程 操作(对外暴露的调用方法) 资源类
* */
public class SaleTicket {
public static void main(String[] args) {
Ticket ticket = new Ticket();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 40; i++) {
ticket.saleTicket();
}
}
}, "A").start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 40; i++) {
ticket.saleTicket();
}
}
}, "B").start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 40; i++) {
ticket.saleTicket();
}
}
}, "C").start();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/21fea80e5db13317e7a2fb6f92799443.png)
![](https://i-blog.csdnimg.cn/blog_migrate/653256cce66fc9882796e10efa093161.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cf28333a0bd09a17a2ac1151d5ba9dc2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4599df966a132c7a456b3ea002af3bf4.png)
package com.dym.jvm;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Ticket //资源类
{
private int number = 30;
private Lock lock=new ReentrantLock();
public void saleTicket() {
lock.lock();
try{
if (number > 0) {
System.out.println(Thread.currentThread().getName() + "\t 卖出第:" + (number--) + "\t 还剩下:" + number);
}
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
/*
* 题目:三个售票员 卖出 30张票
*
* 多线程编程的企业级套路+模板
*
* 在高内聚低耦合的前提下, 线程 操作(对外暴露的调用方法) 资源类
* */
public class SaleTicket {
public static void main(String[] args) {
Ticket ticket = new Ticket();
new Thread(()->{for(int i=1;i<=40;i++) ticket.saleTicket();},"A").start();
new Thread(()->{for(int i=1;i<=40;i++) ticket.saleTicket();},"B").start();
new Thread(()->{for(int i=1;i<=40;i++) ticket.saleTicket();},"C").start();
}
}
lambda Express 快速入门
口诀: 拷贝小括号,写死右箭头,落地大括号
package com.dym.jvm;
interface Foo{
public void sayHello();
}
public class LambdaExpressDemo {
public static void main(String[] args) {
/* Foo foo=new Foo(){
@Override
public void sayHello() {
System.out.println("egrtgyhtg");
}
};
foo.sayHello();*/
Foo foo=()->{ System.out.println("egrtgyhtg-------lambda Express");};
foo.sayHello();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/cb2bd84ed3968dd7fc1487ea94994336.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a2d7edb72f4f028843976752f6b2ca9c.png)
package com.dym.jvm;
interface Foo {
public int add(int x, int y);
default int div(int x, int y) {
System.out.println("减法******hefuerhg");
return x / y;
}
public static int mv(int x,int y){
System.out.println("乘法******hefuerhg");
return x*y;
}
}
public class LambdaExpressDemo {
public static void main(String[] args) {
Foo foo = (int x, int y) -> {
System.out.println("加法-------lambda Express");
return x + y;
};
System.out.println(foo.add(3, 5));
System.out.println(foo.div(10,5));
System.out.println(Foo.mv(3,5));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/e05fca68abbbb5adab338760c99c3061.png)