JAVA中,启动线程的三种方法

1、继承Thread类, 重写run方法,在main函数中,调用start方法。代码如下:

  1. //播放音乐线程类  

  2. class MusicThread extends Thread {  

  3.     public void run() {  

  4.         for (int i = 0; i < 50; i++) {  

  5.             System.out.println("播放音乐" + i);  

  6.         }  

  7.     }  

  8. }  

  9.   

  10. //主线程类  

  11. public class ThreadDemo3 {  

  12.     public static void main(String[] args) {  

  13.   

  14.         //启动播放音乐的线程  

  15.         MusicThread thread1 = new MusicThread();  

  16.         thread1.start();  

  17.   

  18.         //主线程  

  19.         for (int i = 0; i < 50; i++) {  

  20.             System.out.println("打游戏" + i);  

  21.         }  

  22.     }  

  23. }  

 

2、实现Runnable接口,重写run方法,在main函数中,调用start方法。代码如下:

 

  1. //1):定义一个类A实现于java.lang.Runnable接口,注意A类不是线程类.  

  2. class MusicImplements implements Runnable{  

  3.     //2):在A类中覆盖Runnable接口中的run方法.  

  4.     public void run() {  

  5.         //3):在run方法中编写需要执行的操作  

  6.         for(int i = 0; i < 50; i ++){  

  7.             System.out.println("播放音乐"+i);  

  8.         }  

  9.           

  10.     }  

  11. }  

  12.   

  13. public class ImplementsRunnableDemo {  

  14.     public static void main(String[] args) {  

  15.         for(int j = 0; j < 50; j ++){  

  16.             System.out.println("运行游戏"+j);  

  17.             if(j == 10){  

  18.                 //4):在main方法(线程)中,创建线程对象,并启动线程  

  19.                 MusicImplements mi = new MusicImplements();  

  20.                 Thread t = new Thread(mi);  

  21.                 t.start();  

  22.             }  

  23.         }  

  24.     }  

  25.   

  26. }  

3、实现Callable接口,重写call()方法,在main函数中调用start()方法。

import java.util.concurrent.Callable;

public class ThreadImplementsCallable implements Callable<Integer> {

  private int i; 

  @Override

  public Integer call() throws Exception {

    for(; i < 100; i++){

      System.out.println(Thread.currentThread().getName() + " " + i);

    }

    return i;

  }

}

 

import java.util.concurrent.Callable;

import java.util.concurrent.FutureTask;

public class Main {

  public static void main(String[] args) {

    Callable<Integer> callable = new ThreadImplementsCallable();

    FutureTask<Integer> futureTask = new FutureTask<>(callable);

    for(i = 0; i < 100; i++){

      System.out.println(Thread.currentThread().getName() + " " + i);

      if (i == 5) {

        new Thread(futureTask).start();

        new Thread(futureTask).start();

      }

    }

    try {

      System.out.println("futureTask ruturn: " + futureTask.get());

    } catch (Exception e) {

      e.printStackTrace();

    }

  }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值