多线程
多线程
Thread 和 Runnable
1.线程开启不一定立即执行,由CPU调度执行
2.Callable 接口
3.静态代理模式
public class StaticProxy {
public static void main(String[] args) {
weddingcompany ta=new weddingcompany(new You());
ta.HappyMarry();
}
}
interface Marry{
void HappyMarry ();
}
class You implements Marry {
@Override
public void HappyMarry() {
System.out.println("秦老师要结婚了,超开心");
}
}
class weddingcompany implements Marry{
private Marry target;
public weddingcompany(Marry target){
this.target=target;
}
@Override
public void HappyMarry () {
before();
this.target.HappyMarry();
after();
}
private void after(){
System.out.println("结婚之后,收尾款");
}
private void before(){
System.out.println("结婚之前,布置现场");
}
}
4.Lamda表达式
5.线程状态和方法
1.停止线程
2.线程休眠
3.线程礼让
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()+"线程停止");
}
}
4.Join
5.线程优先级
6.守护线程
6.线程同步
采用synchiozed方法
7.死锁
public class DeadLock {
public static void main(String[] args) {
Makeup makeup = new Makeup(0, "1");
Makeup makeup2 = new Makeup(1, "2");
makeup.start();
makeup2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirrot{
}
class Makeup extends Thread{
static Lipstick lipstick=new Lipstick();
static Mirrot mirrot=new Mirrot();
int choice;
String girlName;
Makeup(int choice,String girlName){
this.choice=choice;
this.girlName=girlName;
}
@Override
public void run() {
try {
makeup();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//化妆,互相持有对方的锁
private void makeup() throws InterruptedException {
if(choice==0){
synchronized (lipstick){
System.out.println(this.girlName+"获得口红的锁");
Thread.sleep(1000);
} synchronized (mirrot){
System.out.println(this.girlName+"获得镜子的锁");
}
}else{
synchronized (mirrot){
System.out.println(this.girlName+"获得镜子的锁");
Thread.sleep(2000);
} synchronized (lipstick){
System.out.println(this.girlName+"获得口红的锁");
}
}
}
}
8.Lock
9.线程通信
public class TestPc {
public static void main(String[] args) {
Container container=new Container();
new Producer(container).start();
new Consumer(container).start();
}
}
class Producer extends Thread{
Container container;
public Producer(Container container){
this.container=container;
}
@Override
public void run() {
for (int i = 1; i < 100; i++) {
System.out.println("生产了"+i+"鸡");
try {
container.push(new Chicken(i));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
class Consumer extends Thread{
Container container;
public Consumer(Container container){
this.container=container;
}
@Override
public void run() {
for (int i = 1; i < 100; i++) {
try {
System.out.println("消费了-->"+ container.pop().id+"鸡");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
class Chicken{
int id;
public Chicken(int id){
this.id=id;
}
}
//缓冲区
class Container{
//需要一个容器大小
Chicken[] chickens=new Chicken[10];
int count=0;
public synchronized void push(Chicken chicken) throws InterruptedException {
if(count==chickens.length){
//等待
try{
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
chickens[count++]=chicken;
this.notify();
}
public synchronized Chicken pop() throws InterruptedException {
if(count==0){
//等待生产者生产,消费者等待
try{
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
Chicken chicken=chickens[--count];
this.notify();
return chicken;
}
}
10.线程池
public class TestPool {
public static void main(String[] args) {
//1.创建服务,创建线程池
ExecutorService service= Executors.newFixedThreadPool(10);
service.execute(new MyThread());
service.execute(new MyThread());
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}

浙公网安备 33010602011771号