线程问题

线程问题

记一蠢事

在使用redisson分布式锁的时候,在单元测试测试这个锁的怎么运行的。
既然要测锁,就要用多个线程去测,一想到线程就使用Runable去创建了,因为可以使用内部类直接实现,感觉比较方便。

Runable runable = new Runnable() {
	@Override
	public void run() {
		...
	}
};

感觉还ok,然后

Runable runable1 = new Runnable() {
	@Override
	public void run() {
		...
	}
};

Runable runable2 = new Runnable() {
	@Override
	public void run() {
		...
	}
};

runable1.run();
runable2.run();

就当自己已经跑了两个线程。蠢哭了。。。
这样实际就是实现了两个类,并分别去调用了它的run()方法。没有新开启线程。

创建线程

既然给蠢到了,就看看实际线程是怎么创建的吧

extends Thread

public class MyThread extends Thread {
    
    @Override
    public void run() {
        ...
    }
    
    
    public static void main(String[] args) {
		MyThread mThread1=new MyThread();
		MyThread mThread2=new MyThread();
		MyThread mThread3=new MyThread();
		mThread1.start();
		mThread2.start();
		mThread3.start();
	}
}

implements Runable

public class MyThread implements Runable {
    
    @Override
    public void run() {
        ...
    }
    
    
    public static void main(String[] args) {
        MyThread mThread = new MyThread();
		Thread mThread1 = new Thread(mThread,"1");
		Thread mThread2 = new Thread(mThread,"2");
		Thread mThread3 = new Thread(mThread,"3");
		mThread1.start();
		mThread2.start();
		mThread3.start();
	}
}
public class Main {
    
    public static void main(String[] args) {
        
		Thread mThread1 = new Thread(() -> {
		    ...
		});
		Thread mThread1 = new Thread(() -> {
		    ...
		});
	    mThread1.start();
		mThread2.start();
	}
}

使用线程池 java.util.concurrent

public class Main {
    
    public static void main(String[] args) {
        
	    ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            executorService.execute(() -> {
                System.out.print("run..");
            });
        }
        executorService.shutdown();
	}
}

@Test单元测试 线程测试

若是在单元测试中测试线程问题,你可能会遇到一个问题。
run()方法中是写了输出的,但是控制台上什么都没有。
是因为单元测试在运行时是不管其他线程是否结束的,别的线程还在跑,它可能已经结束了,所以控制台上就没有输出了。
那么就要让测试线程等待其他线程都结束了,最后再结束。

CountDownLatch java.util.concurrent

public class Mian {

    public static void main(String[] args) throws InterruptedException {
        final int totalThread = 10;
        CountDownLatch countDownLatch = new CountDownLatch(totalThread);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < totalThread; i++) {
            executorService.execute(() -> {
                System.out.print("run..");
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println("end");
        executorService.shutdown();
    }
}

每次调用 countDown() 方法会让计数器的值减 1,减到 0 的时候,那些因为调用 await() 方法而在等待的线程就会被唤醒。这里是主线程调用了await() 方法,进入了等待。在计数为0时才会被唤醒。就达到了等待其他线程都结束再执行当前线程的目的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值