java多线程

Multithreading in Java is a very important topic. I have written a lot about Threads in Java. Java Thread is a lightweight process that executes some task. Java provides multithreading support with the Thread class and an application can create multiple threads executing concurrently.

Java中的多线程是一个非常重要的主题。 我已经写了很多有关Java线程的文章。 Java Thread是执行某些任务的轻量级进程。 Java通过Thread类提供了多线程支持,应用程序可以创建多个并发执行的线程。

(Multithreading in Java)

There are two types of threads in an application – user thread and daemon thread. When we start an application, main is the first user thread created and we can create multiple user threads as well as daemon threads. When all the user threads are executed, JVM terminates the program.

应用程序中有两种类型的线程- 用户线程和守护程序线程 。 启动应用程序时,main是创建的第一个用户线程,我们可以创建多个用户线程以及守护程序线程。 当所有用户线程都执行完后,JVM终止程序。

We can set different priorities to different Threads but it doesn’t guarantee that higher priority thread will execute first than lower priority thread. Thread scheduler is the part of Operating System implementation and when a Thread is started, it’s execution is controlled by Thread Scheduler and JVM doesn’t have any control on it’s execution.

我们可以为不同的线程设置不同的优先级,但是不能保证高优先级的线程会比低优先级的线程优先执行。 线程调度程序是操作系统实现的一部分,启动线程时,线程的执行由线程调度程序控制,而JVM对其执行没有任何控制。

We can create Threads by either implementing Runnable interface or by extending Thread Class.

我们可以通过实现Runnable接口或扩展Thread Class来创建线程。

Thread t = new Thread(new Runnable(){
    @Override
    public void run() {
    }
});

Above is a one liner statement to create new Thread, Here we are creating Runnable as Anonymous Class, read this post to learn about inner class, nested class and anonymous inner class.

上面是一个创建新Thread的线性语句,在这里我们将Runnable创建为Anonymous Class,阅读此文章以了解内部类,嵌套类和匿名内部类 。

In last few weeks, I have posted some useful articles on multithreading in java, you can follow them in order to learn about Threads in Java.

在过去的几周中,我发布了一些有关Java多线程的有用文章,您可以关注它们,以了解Java线程。

(Threads in Java)

With Java 8 lambda expressions, we can create Thread in java like below too because Runnable is a functional interface.

使用Java 8 lambda表达式,我们也可以像下面这样在Java中创建线程,因为Runnable是一个功能接口。

Thread t = new Thread(() -> {System.out.println("My Runnable");});
t.start();

Java线程示例 (Java Thread Example)

This is the first post in the multithreading in java tutorial series. Read this to learn about Process and Thread. What is the difference between Thread and Process. Benefits of using Threads and how we can create Threads using Runnable interface and Thread class. This post also compares Runnable interface with Thread class.

这是Java多线程教程系列中的第一篇文章。 阅读本文以了解Process和Thread 。 Thread和Process有什么区别。 使用线程的好处以及如何使用Runnable接口和Thread类创建线程。 这篇文章还比较了Runnable接口和Thread类。

Java线程Hibernate (Java Thread Sleep)

Java Thread sleep is used to pause the execution of current thread. We will use Thread sleep extensively in future posts, so it’s good to know how it works and is it accurate or not?

Java线程睡眠用于暂停当前线程的执行。 我们将在以后的文章中广泛使用Thread sleep,所以很高兴知道它是如何工作的以及它是否准确?

Java线程连接 (Java Thread Join)

Sometimes we need to wait for other threads to finish it’s execution before we can proceed. We can achieve this using Thread join, learn how it works and when we should use it.

有时我们需要等待其他线程完成其执行,然后才能继续。 我们可以使用Thread join来实现这一点,了解它如何工作以及何时使用它。

Java线程状态 (Java Thread States)

Understanding different states of thread is important. Learn how thread changes it’s state and how thread scheduler changes thread state.

了解线程的不同状态很重要。 了解线程如何更改其状态以及线程调度程序如何更改线程状态。

Java线程等待,通知和notifyAll (Java Thread wait, notify and notifyAll)

Java Object class contains three methods using which threads can communicate about the lock status of a resource. Learn with example usage of these Object class methods in a simple Wait-Notify implementation.

Java Object类包含三种方法,线程可以使用这些方法就资源的锁定状态进行通信。 通过简单的Wait-Notify实现,学习这些Object类方法的用法示例。

Java线程安全性和Java同步 (Java Thread Safety and Java Synchronization)

We know that Threads share Object resources, it can lead to data corruption because these operations are not atomic. Learn how we can achieve thread safety in java using different methods. Read this post to learn about the correct usage of synchronization, synchronized methods and synchronized blocks. There are various examples of synchronized usage and the post explains what are the issues with them.

我们知道线程共享对象资源,因为这些操作不是原子操作,可能会导致数据损坏。 了解如何使用不同的方法在Java中实现线程安全 。 阅读这篇文章,以了解正确的同步 ,同步方法和同步块用法。 有各种各样的同步用法示例,该帖子解释了它们的问题。

主线程中的Java异常 (Java Exception in thread main)

JVM creates first thread using main method. This post explains about some common exceptions we see in daily life and what is the root cause of them and how to fix them.

JVM使用main方法创建第一个线程。 这篇文章解释了我们在日常生活中遇到的一些常见异常,它们的根本原因是什么以及如何解决它们。

Singleton类中的线程安全 (Thread Safety in Singleton Class)

In this article, you will learn basic concepts of creating Singleton class. What are thread safety issues with different implementation. How we can achieve thread safety in Singleton class.

在本文中,您将学习创建Singleton类的基本概念。 什么是具有不同实现的线程安全问题。 我们如何在Singleton类中实现线程安全 。

Java中的守护程序线程 (Daemon Thread in Java)

A simple article explaining about daemon threads and how we can create daemon threads in java.

一篇简单的文章,介绍了守护程序线程以及如何在Java中创建守护程序线程。

Java线程本地 (Java Thread Local)

We know that threads share Object’s variables but what if we want to have thread-local variables created at class level. Java provides ThreadLocal utility class to create thread-local variables. Read more to learn about how we can create ThreadLocal variables in java program.

我们知道线程共享对象的变量,但是如果我们想在类级别创建线程局部变量,那该怎么办。 Java提供了ThreadLocal实用程序类来创建线程局部变量。 阅读更多内容以了解如何在Java程序中创建ThreadLocal变量。

Java线程转储 (Java Thread Dump)

Java Thread dump provides the current threads information for the program. Java Thread dump provides useful information to analyze performance issues with the application. You can use thread dump to find and fix deadlock situations. This post explains different methods that can be used to generate thread dump in java.

Java Thread dump提供了程序的当前线程信息。 Java Thread dump提供有用的信息来分析应用程序的性能问题。 您可以使用线程转储来查找和修复死锁情况。 这篇文章解释了可用于在Java中生成线程转储的不同方法。

如何在Java中分析死锁并避免死锁 (How to Analyze Deadlock and avoid it in Java)

Deadlock is a situation where multiple threads are waiting for each other to release resources causing cyclic dependency. This article discusses about the situation in which we can get deadlock in a java program. How we can use Thread dump to find the deadlock and best practices to avoid deadlock in java program.

死锁是指多个线程互相等待释放资源而导致循环依赖的情况。 本文讨论了在Java程序中可能出现死锁的情况。 我们如何使用线程转储查找死锁,以及如何避免Java程序死锁的最佳实践。

Java计时器线程 (Java Timer Thread)

This post explains how we can use Java Timer and TimerTask classes to create jobs to run at scheduled interval, an example program showing it’s usage and how we can cancel the timer.

这篇文章解释了我们如何使用Java Timer和TimerTask类来创建要按计划的时间间隔运行的作业,一个示例程序显示了它的用法以及如何取消计时器。

Java生产者使用者问题 (Java Producer Consumer Problem)

Before Java 5, producer-consumer problem can be solved using wait() and notify() methods but introduction of BlockingQueue has made it very easy. Learn how we can use BlockingQueue to solve producer consumer problem in java.

在Java 5之前,可以使用wait()和notify()方法解决生产者-消费者问题,但是BlockingQueue的引入使其变得非常容易。 了解如何使用BlockingQueue解决Java中的生产者使用者问题。

Java线程池 (Java Thread Pool)

Java Thread Pool is a collection of worker threads waiting to process jobs. Java 5 introduction of Executor framework has made it very easy to create thread pool in java using Executors and ThreadPoolExecutor classes. Learn how to use them to create thread pool in java.

Java线程池是等待处理作业的工作线程的集合。 Java 5对Executor框架的引入使使用Executors和ThreadPoolExecutor类在Java中创建线程池变得非常容易。 了解如何使用它们在Java中创建线程池。

Java可调用的未来 (Java Callable Future)

Sometimes we wish Thread could return values that we can use. Java 5 Callable can be used in that case that is similar as Runnable interface. We can use Executor framework to execute Callable tasks.

有时我们希望Thread可以返回我们可以使用的值。 在这种情况下,可以使用Java 5 Callable,它类似于Runnable接口。 我们可以使用Executor框架执行Callable任务。

Java FutureTask示例 (Java FutureTask Example)

FutureTask class is the base concrete class that implements Future interface. We use it with Callable implementation and Executors for asynchronous processing. FutureTask provide implementation methods to check the state of the task and return the value to the calling program once it’s processing is finished. It comes handy when you want to override some of the implementation methods of the Future interface.

FutureTask类是实现Future接口的基础具体类。 我们将其与Callable实现和Executors一起用于异步处理。 FutureTask提供实现方法来检查任务的状态,并在处理完成后将值返回给调用程序。 当您要覆盖Future接口的某些实现方法时,它很方便。

This list will keep growing as I write more on multithreading in java, so make sure to bookmark it for future use.

随着我在Java中编写更多有关多线程的内容,此列表将不断增长,因此请确保将其添加书签以供将来使用。

翻译自: https://www.journaldev.com/1079/multithreading-in-java

java多线程