阿里面试官:你连个java多线程都说不清楚,我招你进来干什么

时间:2020-09-09 19:11:27   收藏:0   阅读:47

创建线程的方法

public class TestThread1 extends Thread{
    @override
    public void run(){
        System.out.println("线程run方法!");
    }
    
    public static void main(String){
        new TestThread1().start();
    }
}

public class TestThread2 implements Runnable{
	@Override
	public void run() {
		System.out.println("线程run方法!");
	}
	
	public static void main(String[] args) {
		new Thread(new TestThread2()).start();
	}
}


*   创建执行服务

*   提交执行

*   获取结果

*   关闭服务
public class TestThread2 implements Callable{
	@Override
	public Boolean call() {
		System.out.println("线程call方法!");
		return true;
	}
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		
		TestThread2 t1 = new TestThread2();
		TestThread2 t2 = new TestThread2();
		TestThread2 t3 = new TestThread2();
		//创建执行服务
		ExecutorService ser = Executors.newFixedThreadPool(3);
		//提交执行
		Future<Boolean> r1 = ser.submit(t1);
		Future<Boolean> r2 = ser.submit(t2);
		Future<Boolean> r3 = ser.submit(t3);
		//获取结果
		boolean rs1 = r1.get();
		boolean rs2 = r2.get();
		boolean rs3 = r3.get();
		//关闭服务
		ser.shutdownNow();
	}

线程同步

1. synchronized修饰方法,线程安全方法

public class TestThreadSafe {

    public static void main(String[] args) {
        BuyTicket bt1 = new BuyTicket();

        Thread thread1 = new Thread(bt1,"张三");
        Thread thread2 = new Thread(bt1,"李四");
        Thread thread3 = new Thread(bt1,"黄牛");

        thread1.start();
        thread2.start();
        thread3.start();
    }

}

class BuyTicket implements Runnable{

    private int ticketNumber = 10;

    private boolean flag = true;

    @Override
    public void run() {
        while(flag) {
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void buy() throws InterruptedException {
        //买票
        if(ticketNumber <= 0){
            System.out.println("票卖完了!");
            flag = false;
            return;
        }
        Thread.sleep(100);
        //Thread.yield();
        System.out.println(Thread.currentThread().getName() + "买到了一张票,还剩下"+(--ticketNumber) + "张票!");
    }
}

2. synchronized修饰代码块,线程安全代码块

public class TestThreadSafe {

    public static void main(String[] args) {
        BuyTicket bt1 = new BuyTicket();

        Thread thread1 = new Thread(bt1,"张三");
        Thread thread2 = new Thread(bt1,"李四");
        Thread thread3 = new Thread(bt1,"黄牛");

        thread1.start();
        thread2.start();
        thread3.start();
    }

}

class BuyTicket implements Runnable{

    private int ticketNumber = 10;

    private boolean flag = true;

    @Override
    public void run() {
        while(flag) {
            System.out.println(Thread.currentThread().getName() + "准备买票" + flag);
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void buy() throws InterruptedException {
        synchronized(this){
            //买票
            if(ticketNumber <= 0){
                flag = false;
                System.out.println("票卖完了!");
                return;
            }
            Thread.sleep(100);
            //Thread.yield();
            System.out.println(Thread.currentThread().getName() + "买到了一张票,还剩下"+(--ticketNumber) + "张票!");
        }
    }
}

3. 使用可重复锁ReentrantLock

import java.util.concurrent.locks.ReentrantLock;

public class TestLock {

    public static void main(String[] args) {
        BuyTicket bt1 = new BuyTicket();

        Thread thread1 = new Thread(bt1,"张三");
        Thread thread2 = new Thread(bt1,"李四");
        Thread thread3 = new Thread(bt1,"黄牛");

        thread1.start();
        thread2.start();
        thread3.start();
    }

}

class BuyTicket implements Runnable{

    private int ticketNumber = 1000;

    private boolean flag = true;

  //定义可重复锁
    private	final ReentrantLock lock = new ReentrantLock();
    
    @Override
    public void run() {
        while(flag) {
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void buy() throws InterruptedException {
        	lock.lock();
            //买票
            if(ticketNumber <= 0){
                System.out.println("票卖完了!");
                flag = false;
            }else {
            	Thread.sleep(100);
                //Thread.yield();
                System.out.println(Thread.currentThread().getName() + "买到了一张票,还剩下"+(--ticketNumber) + "张票!");
            }
            lock.unlock();
    }
}


线程状态

线程(Thread类)方法

守护线程

         eg:后台记录操作日志,监控内存,垃圾回收等待

最后

感谢你看到这里,看完有什么的不懂的可以在评论区问我,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!