1. 继续Thread的类,重新run方法 ```java public class ExtendsThread extends Thread {

      @Override public void run() {

      1. System.out.println(Thread.currentThread().getName() + ",我是子线程");

      }

      public static void main(String[] args) {

      1. System.out.println(Thread.currentThread().getName() + ",我是主线程");
      2. new ExtendsThread().run();
      3. ExtendsThread thread = new ExtendsThread();
      4. thread.start();
      5. thread.interrupt();

      }

    }

    1. 2. 实现Runnable接口,重写run方法
    2. ```java
    3. public class ImplementRunnable implements Runnable {
    4. @Override
    5. public void run() {
    6. // TODO Auto-generated method stub
    7. System.out.println(Thread.currentThread().getName() + ",我是子线程");
    8. }
    9. public static void main(String[] args) {
    10. System.out.println(Thread.currentThread().getName() + ",我是主线程");
    11. new Thread(() -> System.out.println(Thread.currentThread().getName() + "----")).start();
    12. //
    13. Thread thread = new Thread(new ImplementRunnable());
    14. thread.setName("zs");
    15. thread.start();
    16. new Thread(new Runnable() {
    17. @Override
    18. public void run() {
    19. // TODO Auto-generated method stub
    20. System.out.println(Thread.currentThread().getName() + ",我是子线程");
    21. }
    22. }).start();
    23. }
    1. 可以带返回结果的的线程 Callable Future

      1. public class MyCallable implements Callable<String> {
      2. @Override
      3. public String call() throws Exception {
      4. // TODO Auto-generated method stub
      5. return "异步执行发送短信";
      6. }
      7. public static void main(String[] args) {
      8. MyCallable myCallable = new MyCallable();
      9. try {
      10. String call = myCallable.call();
      11. System.out.println(call);
      12. } catch (Exception e) {
      13. // TODO Auto-generated catch block
      14. e.printStackTrace();
      15. }
      16. FutureTask<String> futureTask = new FutureTask<String>(new MyCallable());
      17. new Thread(futureTask).start();
      18. try {
      19. System.out.println("result: " + futureTask.get());
      20. } catch (InterruptedException e) {
      21. // TODO Auto-generated catch block
      22. e.printStackTrace();
      23. } catch (ExecutionException e) {
      24. // TODO Auto-generated catch block
      25. e.printStackTrace();
      26. }
      27. }
      28. }
    2. 线程池批量创建线程

      1. public class exectorCreateThread {
      2. public static void main(String[] args) {
      3. ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
      4. new SynchronousQueue<Runnable>());
      5. executor.execute(new Thread(() -> {
      6. System.out.println(Thread.currentThread().getName() + ",我是线程池创建");
      7. }));
      8. }
      9. }
    3. 其他扩展方式创建线程: spring提供异步直接@Asyn

    多个线程交替打印数字,顺序打印

    1. public class Test {
    2. public static void main(String[] args) {
    3. Data data = new Data();
    4. new Thread(() -> {
    5. for (int i = 0; i < 10; i++) {
    6. data.printA();
    7. }
    8. }, "A").start();
    9. new Thread(() -> {
    10. for (int i = 0; i < 10; i++) {
    11. data.printB();
    12. }
    13. }, "B").start();
    14. new Thread(() -> {
    15. for (int i = 0; i < 10; i++) {
    16. data.printC();
    17. }
    18. }, "C").start();
    19. }
    20. }
    21. class Data{
    22. Lock lock = new ReentrantLock();
    23. // Condition 可以保障精准通知
    24. Condition condition1 = lock.newCondition();
    25. Condition condition2 = lock.newCondition();
    26. Condition condition3 = lock.newCondition();
    27. private int num = 1;
    28. private int size = 0;
    29. public void printA() {
    30. lock.lock();
    31. try {
    32. while (num != 1) {
    33. condition1.await();
    34. }
    35. System.out.println(Thread.currentThread().getName() + ":" + size ++);
    36. num = 2;
    37. condition2.signal();
    38. } catch (InterruptedException e) {
    39. e.printStackTrace();
    40. } finally {
    41. lock.unlock();
    42. }
    43. }
    44. public void printB() {
    45. lock.lock();
    46. try {
    47. while (num != 2) {
    48. condition2.await();
    49. }
    50. System.out.println(Thread.currentThread().getName() + ":" + size ++);
    51. num = 3;
    52. condition3.signal();
    53. } catch (InterruptedException e) {
    54. e.printStackTrace();
    55. } finally {
    56. lock.unlock();
    57. }
    58. }
    59. public void printC() {
    60. lock.lock();
    61. try {
    62. while (num != 3) {
    63. condition3.await();
    64. }
    65. System.out.println(Thread.currentThread().getName() + ":" + size ++);
    66. num = 1;
    67. condition1.signal();
    68. } catch (InterruptedException e) {
    69. e.printStackTrace();
    70. } finally {
    71. lock.unlock();
    72. }
    73. }
    74. }