Implementation method of multi-threading based on java learning
这篇文章介绍基于java学习中多线程的实现方法
1 继承Thread类
继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实例,它代表一个线程的实例,并且,启动线程的唯一方法就是通过Thread类的start()实例方法。start()方法是一个native方法,它将启动一个新线程,并执行run()方法。这种方式实现多线程很简单,通过自己的类直接extend Thread,并复写run()方法,就可以启动新线程并执行自己定义的run()方法。
public class MyThread extends Thread { public void run() { System.out.println("MyThread.run()"); } } //启动线程 MyThread myThread1 = new MyThread(); MyThread myThread2 = new MyThread(); myThread1.start(); myThread2.start();
2 实现Runnable接口
如果一个类继承另一个类,就无法继承Thread类(java中不支持多继承),这样就要实现Runnable接口,在实现类中重写run()方法,在main方法中实现多线程:实例化一个实现类对象,并初始化Thread类的对象,把实现类传入到Thread的对象里。事实上,当传入一个Runnable target参数给Thread后,Thread的run()方法就会调用target.run()。
//实现Runnable接口 public class MyThread extends OtherClass implements Runnable { public void run() { System.out.println("MyThread.run()"); } } //复写run()方法 public void run() { if (target != null) { target.run(); } } //实例化对象 MyThread myThread = new MyThread(); Thread thread = new Thread(myThread); thread.start();
3 使用ExecutorService、Callable、Future实现有返回结果的多线程
该部分内容还未学到占时不作记录,以后补充
The above is the detailed content of Implementation method of multi-threading based on java learning. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
