实现多线程四种方式之--实现callable接口创建线程

class MyThread implements Callable<Integer>{

     @Override
     public Integer call() throws Exception {
        System. out .println("------->Callable" );
         return new Random().nextInt(50);
    }

}

public class Demo0 {

     public static void main(String[] args) throws InterruptedException, ExecutionException {

        MyThread myThread = new MyThread();

         //因为这里的FutureTask实现了runnable接口,所以可以把FutureTask提交给Executor执行
         FutureTask futureTask = new FutureTask(myThread);

         new Thread(futureTask).start();

         //可以通过FutureTask的get()方法来获取执行线程得到的返回值
        Integer result = (Integer)futureTask.get();

        System. out .println(result);

    }
}
  • 从上面代码可以得出,实现callable接口我们可以获得线程的执行结果,并且得到线程执行过程中的异常信息。

  • 生产者,消费者实例

//共享数据的封装
class ShareData01{

     private int number =0;
    Lock lock = new ReentrantLock();
    Condition condtion = lock.newCondition();

     public void increament() throws InterruptedException{
         lock.lock();
         try {
             //对于某一个参数的版本,实现中断和虚假唤醒是可能的,而且wait()方法应始终在循环中使用,这样即使循环中
             //有多个阻塞状态的线程,当全部唤醒后,也只会有一个执行此方法
              while (number !=0){
                 condtion.await(); //this.wait()
            }
            ++ number;
            System. out .println(Thread.currentThread().getName()+ ":" +number );
             condtion.signalAll(); //this.notify()
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
             lock.unlock();
        }
    }

public void decreament() throws InterruptedException{

       lock.lock();
         try {
             while (number ==0){
                 condtion.await();
            }
            -- number;
            System. out .println(Thread.currentThread().getName()+ ":" +number );
             condtion.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
             lock.unlock();
        }
    }


}
//四个线程的生产者,消费者实例
public class Demo02 {

     public static void main(String[] args) {

         final ShareData01 shareData = new ShareData01();

         new Thread(new Runnable(){
             @Override
             public void run() {
                 for (int i = 0; i <10; i++) {
                     try {
                        Thread. sleep(1000);
                        shareData.increament();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "AA" ).start();

         new Thread(new Runnable(){
             @Override
             public void run() {
                 for (int i = 0; i <10; i++) {
                     try {
                        Thread. sleep(1000);
                        shareData.increament();
                    } catch (InterruptedException e) {
                         // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }, "BB" ).start();

         new Thread(new Runnable(){
             @Override
             public void run() {
                 for (int i = 0; i <10; i++) {
                     try {
                        Thread. sleep(1000);
                        shareData.decreament();
                    } catch (InterruptedException e) {
                         // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }, "CC" ).start();

         new Thread(new Runnable(){
             @Override
             public void run() {
                 for (int i = 0; i <10; i++) {
                     try {
                        Thread. sleep(1000);
                        shareData.decreament();
                    } catch (InterruptedException e) {
                         // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }, "DD" ).start();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独行客-编码爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值