函数式接口:
任何接口,如果只包含唯一一个抽象方法,那么他就是一个函数式接口
public interface Runnable{
public abstract void run();
}
package lambda;
/*
推导Lambda表达式
*/
public class TestLambda01 {
//3.静态内部类
static class Like2 implements Ilike{
@Override
public void lambda() {
System.out.println("i like lambda2!");
}
}
public static void main(String[] args) {
Ilike ilike=new Like();
ilike.lambda();
ilike=new Like2();
ilike.lambda();
}
}
//1.定义一个函数式接口
interface Ilike{
void lambda();
}
//2.实现类
class Like implements Ilike{
@Override
public void lambda() {
System.out.println("i like lambda!");
}
}
package lambda;
public class TestLambda02 {
//2局部内部类,放到方法里
/*static class Love implements Ilove{
@Override
public void love(int a) {
System.out.println("i love you--> "+a);
}
}*/
public static void main(String[] args) {
/*class Love implements Ilove{
@Override
public void love(int a) {
System.out.println("i love you--> "+a);
}
}*/
//3,匿名内部类
/* Ilove love=new Love();*/
// Ilove love=new Ilove() 4.Lambda替换
Ilove love=(int a,int b)-> {
System.out.println("i love you--> "+a);
};
//5,再简化.去掉参数类型
love=(a,b)->{
System.out.println("i love you--> "+a);
};
//6.继续简化 去掉括号
love=(a,b)->{
System.out.println("i love you--> "+a);
System.out.println("i love you too --> "+b);
};
//7.简化 去掉花括号
love=(a,b)-> System.out.println("i love you--> "+a);
love.love(520,521);
//总结;
//lambda表达式只有一行代码的情况下才可以简化成一行,如果有多行,那么就使用代码块包裹
//前提是接口是函数式接口
//多个参数也可以去掉参数类型。要去就都去掉。。必须加括号
}
}
interface Ilove{
void love(int a,int b);
}
//1.外部类放到内部加 static
/*class Love implements Ilove{
@Override
public void love(int a) {
System.out.println("i love you--> "+a);
}
}*/
package lambda;
public class TestLambda02 {
//2局部内部类,放到方法里
/*static class Love implements Ilove{
@Override
public void love(int a) {
System.out.println("i love you--> "+a);
}
}*/
public static void main(String[] args) {
/*class Love implements Ilove{
@Override
public void love(int a) {
System.out.println("i love you--> "+a);
}
}*/
//3,匿名内部类
/* Ilove love=new Love();*/
// Ilove love=new Ilove() 4.Lambda替换
Ilove love=(int a,int b)-> {
System.out.println("i love you--> "+a);
};
//5,再简化.去掉参数类型
love=(a,b)->{
System.out.println("i love you--> "+a);
};
//6.继续简化 去掉括号
love=(a,b)->{
System.out.println("i love you--> "+a);
System.out.println("i love you too --> "+b);
};
//7.简化 去掉花括号
love=(a,b)-> System.out.println("i love you--> "+a);
love.love(520,521);
//总结;
//lambda表达式只有一行代码的情况下才可以简化成一行,如果有多行,那么就使用代码块包裹
//前提是接口是函数式接口
//多个参数也可以去掉参数类型。要去就都去掉。。必须加括号
}
}
interface Ilove{
void love(int a,int b);
}
//1.外部类放到内部加 static
/*class Love implements Ilove{
@Override
public void love(int a) {
System.out.println("i love you--> "+a);
}
}*/
线程停止
package Thread;
//测试stop
//1.建议线程正常停止--》利用次数,不建议死循环
//2.建议使用标志位-》设置一个编标志位
//3.不要使用stop或者destory等JDK不建议的方法
public class TestStop implements Runnable {
//1.设置一个标志位
private boolean flag=true;
@Override
public void run() {
int i=0;
while (flag){
System.out.println("run ----Thread "+i++);
}
}
//设置一个公开的方法停止线程,转换标志位
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
TestStop testStop=new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main "+i);
if (i==900){
//调用stop方法切换标志位,让线程停止
testStop.stop();
System.out.println("线程停止了!!!");
}
}
}
}
线程方法
线程状态:创建状态 就绪状态 阻塞状态 运行状态 死亡状态
setPriority(int newPriority) 更改线程优先级
static void sleep(long millis) 在指定的毫秒数内让正在执行的线程体休眠
void join() 等待该线程终止
static void yield() 暂停当前正在执行的线程对象,并执行其他线程
void interrupt() 中断线程,最好别用
boolean isAlive() 测试线程是否处于活动状态
线程休眠
!!!每个对象都有一个锁,sleep不会释放锁
package Thread;
public class TestSleep implements Runnable{
//1000毫秒 =1秒
//!!!每个对象都有一个锁,sleep不会释放锁
//模拟网络延时:放大问题发生性
private int ticketNums=10;
@Override
public void run() {
while (true){
if (ticketNums<=0){
break;
}
try {
//模拟延时
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNums--+"张票");
}
}
public static void main(String[] args) {
Thread04 ticket=new Thread04();
new Thread(ticket,"小明").start();
new Thread(ticket,"小王").start();
new Thread(ticket,"小李").start();
}
}
package Thread;
import java.text.SimpleDateFormat;
import java.util.Date;
//模拟倒计时
public class TestSleep2 {
public static void tenDown() throws InterruptedException {
//打印当前时间
Date startTime=new Date(System.currentTimeMillis());//获取系统当前时间
int num=10;
while (true){
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime=new Date(System.currentTimeMillis());//更新时间
System.out.println(num--);
if (num<=0){
break;
}
}
}
public static void main(String[] args) {
try {
tenDown();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
线程礼让
让当前正在执行的线程暂停,但不阻塞
让线程从运行状态转为就绪状态
让cpu重新调度,礼让不一定成功,看cpu心情
package Thread;
//测试礼让线程
//礼让不一定成功。看cpu心情
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()+"线程停止执行");
}
}
线程强制执行
join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞
package Thread;
//测试join方法/
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin=new TestJoin();
Thread thread=new Thread(testJoin);
thread.start();
//主线程
for (int i = 0; i < 1000; i++) {
if (i==200){
thread.join();//插队
}
System.out.println("main !"+i );
}
}
}
观测线程状态
Thread.State
NEW 尚未启动
RUNNABLE 在Java虚拟机中执行
BLOCKED 被阻塞等待监视器锁定
WAITING 正在等待另一个线程执行特定动作
TIMED.WAITING 正在等待另一个线程执行特定动作达到指定等待时间
TERMINATED 已退出执行的线程
package Thread;
//观察线程状态
public class Threadstate {
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);
thread.start();//启动线程
thread.getState();
System.out.println(state);//run
while(state!=Thread.State.TERMINATED){//只要线程不中止,就一直输出状态/
Thread.sleep(100);
state=thread.getState();//更新线程状态
System.out.println(state);//输出状态
}
//thread.start();//会报错,线程不能启动两次!!
}
}
线程优先级
优先级范围 1—10
Thread.MIN_PRIORITY=1;
Thread.MAX_PRIORITY=10;
Thread.NORMAL_PRIORITY=5;
使用以下方法改变或者获取优先级
getPriority().setPriority(int XXX)
package Thread;
public class TestPriority {
public static void main(String[] args) {
//主线程优先级默认
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
Mypriority mypriority=new Mypriority();
Thread t1=new Thread(mypriority);
Thread t2=new Thread(mypriority);
Thread t3=new Thread(mypriority);
Thread t4=new Thread(mypriority);
Thread t5=new Thread(mypriority);
//先设置优先级,再启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);
t4.start();
t5.setPriority(Thread.MIN_PRIORITY);
t5.start();
}
}
class Mypriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}
守护线程(daemon)
线程分为用户线程和守护线程
虚拟机必须确保用户线程执行完毕
虚拟机不用等待守护线程执行完毕
如,后台记录操作日志,监控内存,垃圾回收
人生不过三万天
package Thread;
//测试守护线程
//上帝守护你
public class Testdemo {
public static void main(String[] args) {
God god=new God();
You1 you1=new You1();
Thread thread=new Thread(god);
thread.setDaemon(true);//默认是false表示用户线程,正常的线程都是用户线程。。。
thread.start();//上帝守护线程启动
new Thread(you1).start();//你 用户线程启动
}
}
//上帝
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝保佑着你");
}
}
}
//你
class You1 implements Runnable{
@Override
public void run() {
for (int i = 0; i <36500 ; i++) {
System.out.println("你一生都在开心的活着");
}
System.out.println("goodbye world!");//hello world
}
}
线程同步(等待机制)
多个线程操作同一个资源
并发:同一个对象被多个线程同时操作
等待池形成队列和锁机制(队列和锁)
package Thread.syn;
//不安全买票
public class UnSafeByTicket {
public static void main(String[] args) {
BuyTicket station=new BuyTicket();
new Thread(station,"我").start();
new Thread(station,"你").start();
new Thread(station,"小明").start();
}
}
class BuyTicket implements Runnable{
private int ticketNums=10;//票
boolean flag=true;
@Override
public void run() {
//买票
while (flag){
try {
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void buy() throws InterruptedException {
//判断是否有票
if (ticketNums<=0){
flag=false;
return;
}
//模拟延时
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--);
}
}
package Thread.syn;
//不安全取钱
//两个人去取钱,账户
public class UnSafeBank {
public static void main(String[] args) {
//账户
Account account=new Account(100,"结婚基金");
Drawing you =new Drawing(account,50,"你");
Drawing youwife =new Drawing(account,100,"你wife");
you.start();
youwife.start();
}
}
//账户
class Account{
int money;//余额
String name;//卡名
public Account(int money, String name) {
this.money = money;
this.name = name;
}
}
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;
}
//sleep 可以放大问题的发生行
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
account.money=account.money-drawingMoney;
nowMoney=nowMoney+drawingMoney;
System.out.println(account.name+"余额为:"+account.money);
System.out.println(this.getName()+"手里的钱:"+nowMoney);
}
}
package Thread.syn;
import java.util.ArrayList;
import java.util.List;
//线程不安全的集合
public class UnSafeList {
public static void main(String[] args) throws InterruptedException {
List<String> list=new ArrayList<String>();
for (int i = 0; i < 10000; i++) {
new Thread(()->{
list.add(Thread.currentThread().getName());
}).start();
}
Thread.sleep(3000);
System.out.println(list.size());
}
}