- -多线程的六种状态: 新建状态-(即创建线程对象) 就绪状态-(即调用start方法) 阻塞状态-(即无法获得锁对象) 等待状态-(遇到wait方法) 计时状态(即遇到sleep方法) 结束状态(即代码运行完毕)
- 线程池:Executors.newCachedThreadPool() ,默认创建一个空的池子即线程池, 当有任务时会自动创建线程对象,当任务执行完毕后,线程对象不会消失,回到线程池即重复使用
public class Test01 {
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"在执行");
}
});
executorService.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"在执行");
}
});
executorService.shutdown();
ExecutorService executorService1 = Executors.newFixedThreadPool(10);
executorService1.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"在执行");
}
});
executorService1.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"在执行");
}
});
executorService1.shutdown();
}
}
public class Test02 {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor =
new ThreadPoolExecutor(2,
6,
4,
TimeUnit.DAYS,new ArrayBlockingQueue<>(10),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 19; i++) {
int y = i;
threadPoolExecutor.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+y+"在执行");
}
});
}
}
}
- volatil关键字:即强制线程每次使用时都会查看共享区域最新数据
public class Boy extends Thread {
@Override
public void run() {
try {
Thread.sleep(100);
share.money=90000;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class girl extends Thread {
@Override
public void run() {
while (share.money==100000){
}
System.out.println("结婚基金不是十万了");
}
}
public class share {
public static volatile int money = 100000;
}
public class Test01 {
public static void main(String[] args) {
girl girl = new girl();
girl.start();
Boy boy = new Boy();
boy.start();
}
}
- 原子性:即一次性或多个操作,要么所有操作步骤成功且不受外界干扰而失败,要么操作全部失败,即一件事分为多个步骤,要么成功,要么失败
- count++ 并不是一个原子性操作,即在运行过程中,有可能被其它线程抢走cpu使用权
public class Test02 {
public static void main(String[] args) {
AtomicInteger atomicInteger = new AtomicInteger(10);
int i = atomicInteger.get();
System.out.println(i);
int andDecrement = atomicInteger.getAndIncrement();
System.out.println(andDecrement);
System.out.println(atomicInteger.get());
int i1 = atomicInteger.incrementAndGet();
System.out.println(i1);
int i2 = atomicInteger.addAndGet(6);
System.out.println(i2);
int andSet = atomicInteger.getAndSet(12);
System.out.println(andSet);
System.out.println(atomicInteger.get());
}
}
- hashmap集合,线程不安全, hashtable集合: 能保证线程安全,但效率慢 ConcurrentHashMap集合: 既能保证线程安全运行效率相对也快
public class Test03 {
public static void main(String[] args) {
ConcurrentMap<String,String> concurrentMap = new ConcurrentHashMap<>();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
concurrentMap.put(i+ " ",i+" ");
}
}
}) ;
thread.start();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 9; i < 110; i++) {
concurrentMap.put(i+ " ",i+" ");
}
}
}) ;
thread1.start();
}
}
public class Test04 {
public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(3);
Parents parents = new Parents(countDownLatch);
parents.setName("小美");
parents.start();
children1 children1 = new children1(countDownLatch);
children1.setName("小明");
children1 children2 = new children1(countDownLatch);
children2.setName("小李");
children1 children3 = new children1(countDownLatch);
children3.setName("小王");
children1.start();
children2.start();
children3.start();
}
}
ublic class Parents extends Thread {
private CountDownLatch countDownLatch;
public Parents(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("吃完了,收拾碗筷");
}
}
public class children1 extends Thread {
private final CountDownLatch countDownLatch;
public children1(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
for (int i = 1; i <=10; i++) {
System.out.println(getName()+"已经吃了"+i+"了饺子了");
}
countDownLatch.countDown();
}
}
public class children2 extends Thread {
private final CountDownLatch countDownLatch;
public children2(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
for (int i = 1; i <=12; i++) {
System.out.println(getName()+"已经吃了"+i+"了饺子了");
}
countDownLatch.countDown();
}
}
public class children3 extends Thread {
private final CountDownLatch countDownLatch;
public children3(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
for (int i = 1; i <=15; i++) {
System.out.println(getName()+"已经吃了"+i+"了饺子了");
}
countDownLatch.countDown();
}
}
- Semaphore 可以控制访问特定资源的线程数量
public class waitfor implements Runnable {
Semaphore semaphore = new Semaphore(5);
@Override
public void run() {
try {
semaphore.acquire();
System.out.println("获得通行证,并开始行驶");
Thread.sleep(2000);
System.out.println("收回通行证");
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Test05 {
public static void main(String[] args) {
waitfor waitfor = new waitfor();
for (int i = 0; i < 50; i++) {
Thread thread = new Thread(waitfor);
thread.start();
}
}
}