多线程

   
public class MyThread extends Thread{ @Override  public void run() {  for (int i = 0; i < 100; i++) {  System.out.println("线程开了"+i);
        }
   }
}
Thread th = new MyThread();
th.start();

MyRunnable mr = new MyRunnable(); Thread t1 = new Thread(mr); t1.start();
public class MyRunnable implements Runnable{ @Override  public void run() { for (int i = 0; i < 100; i++) { System.out.println("线程开了"+i);
        }
    }
}
public class MyCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
        }
        return "答应";
    }
}
MyCallable mc = new MyCallable();
FutureTask<String> f = new FutureTask<>(mc);
Thread t1 = new Thread(f);
t1.start();
//接收call方法返回值
String s = f.get();
System.out.println(s);
    




public class MyThread implements Runnable {
    private int ticketCount = 100;

    @Override
    public void run() {
        while (true) {
            //同步方法
            if ("一".equals(Thread.currentThread().getName())) {
                boolean b = synchronizedMethod();
                if(b){
                    break;
                }
            }
            //同步代码块
            if ("二".equals(Thread.currentThread().getName())) {
                synchronized () {
                    if (ticketCount == 0) {
                        break;
                    } else {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        ticketCount--;
                        System.out.println(Thread.currentThread().getName() + "票还有" + ticketCount);
                    }
                }
            }
        }
    }

    private static synchronized boolean synchronizedMethod() {
        if (ticketCount == 0) {
            return true;
        } else {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticketCount--;
            System.out.println(Thread.currentThread().getName() + "票还有" + ticketCount);
            return false;
        }
    }
}
public class demo {
    public static void main(String[] args) {

        MyThread mt = new MyThread();
        Thread t1 = new Thread(mt);
        Thread t2 = new Thread(mt);
        t1.setName("一");
        t2.setName("二");
        t1.start();
        t2.start();
    }
}


ReentrantLock lock = new ReentrantLock();





多线程&volatile

ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(()->{
            System.out.println("在执行了"+Thread.currentThread().getName());
        });
executorService.shutdownNow();
//参数是线程最大值
ExecutorService executorService = Executors.newFixedThreadPool(10);
ThreadPoolExecutor pool = (ThreadPoolExecutor) executorService;
System.out.println(pool.getPoolSize()); //当前线程数量
executorService.submit(
    () -> { ...}
);
executorService.shutdown();
   
ThreadPoolExecutor pool = new ThreadPoolExecutor (2, 5, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        
//方法一
pool.submit(()->{...});
//方法二
pool.submit(new MyRunnable());
pool.submit(new MyRunnable());//新建MyRunnable类,实现Runnable,重写run()方法

pool.shutdown();

原子性&并发工具类


AtomicInteger ac = new AtomicInteger(0);

//        HashMap<String, String> hm = new HashMap<>();  线程不安全
//        Hashtable<String, String> hm = new Hashtable<>(); 线程安全,效率低
        ConcurrentHashMap<String, String> hm = new ConcurrentHashMap<>(); //线程安全,效率较高
CountDownLatch countDownLatch = new CountDownLatch(3);



Semaphore

Semaphore semaphore = new Semaphore(2);
public class Semaphore1 implements Runnable{
    Semaphore semaphore = new Semaphore(2);
    @Override
    public void run() {
        try {
            semaphore.acquire();
            System.out.println("获得通行证11111111");
            Thread.sleep(2000);
            System.out.println("归还通行证");
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class demo {
    public static void main(String[] args) {
        Semaphore1 s1 = new Semaphore1();
        for (int i = 0; i < 100; i++) {
            new Thread(s1).start();
        }
    }
}