今天需要快速对多线程明白个大概。。。

本文介绍了多线程的基本概念,包括进程与线程的区别、线程的创建与启动、中断机制以及线程的状态。强调了不要直接调用`run`方法而应使用`start`来启动新线程,并讨论了`interrupt`方法在请求线程终止时的作用。还提到了线程池的重要性和`Executors`类的应用,以及同步、阻塞和线程安全集合的概念。
摘要由CSDN通过智能技术生成

我不得不说,每次都是任务到跟前了,才不得不迅速的去搞明白某个东西。。。

这种状态真不好。。。

 

多进程与多线程本质的区别在于每个进程拥有自己的一整套变量。而线程则共享数据。

 

一个最简单的使用线程的例子。

 

1,将任务代码移动到实现了runnable接口的类的run方法中:

class MyRunnable implments Runnable{

   public void run(){

      task code;

   }

}

 

2, 创建一个类对象:

Runnable r = new MyRunnable();

 

3, 用runnable创建一个Thread对象:

Thread t = new Thread(r);

 

4,启动线程:

t.start();

 

CAUTION: Do not call the run method of the Thread class or the Runnable object. Calling the
run method directly merely executes the task in the same thread—no new thread is started.
Instead, call the Thread.start method. It will create a new thread that executes the run
method.

 

下面这段话太重要了,直接贴上来。。。

Interrupting Threads
A thread terminates when its run method returns, by executing a return statement, after
executing the last statement in the method body, or if an exception occurs that is not
caught in the method. In the initial release of Java, there also was a stop method that
another thread could call to terminate a thread. However, that method is now deprecated.
We discuss the reason in the section “Why the stop and suspend Methods Are Deprecated”
on page 762.
There is a way to force a thread to terminate. However, the interrupt method can be
used to request termination of a thread.
When the interrupt method is called on a thread, the interrupted status of the thread is set.
This is a boolean flag that is present in every thread. Each thread should occasionally
check whether it has been interrupted.
To find out whether the interrupted status was set, first call the static Thread.currentThread
method to get the current thread and then call the isInterrupted method:
while (!Thread.currentThread().isInterrupted() && more work to do)
{
do more work
}

 

如果循环内使用了sleep,则不能使用isInterrupted()来检测。

(P728 Core Java volume 1)

 

Thread States
Threads can be in one of six states:
• New
• Runnable (means thread is runnable or the thread is running)
• Blocked
• Waiting
• Timed waiting
• Terminated

(P731 仔细看每种状态的说明)

Preemptive scheduling systems and cooperative scheduling(记得在sql server中的那一段论述吗?)

 

On a machine with multiple processors, each processor can run a thread, and you can
have multiple threads run in parallel. Of course, if there are more threads than processors,
the scheduler still has to do time-slicing.
Always keep in mind that a runnable thread may or may not be running at any given
time. (This is why the state is called “runnable” and not “running.”)

 

Thread Properties
daemon threads, thread groups, and handlers for uncaught exceptions.

 

You should certainly never structure your programs so that their
correct functioning depends on priority levels.

 

同步,阻塞,锁,Thread-Safe Collections

 

Executors 这个重点,要仔细看
Constructing a new thread is somewhat expensive because it involves interaction with
the operating system. If your program creates a large number of short-lived threads,
then it should instead use a thread pool. A thread pool contains a number of idle threads
that are ready to run. You give a Runnable to the pool, and one of the threads calls the run
method. When the run method exits, the thread doesn’t die but stays around to serve the
next request.

The Executors class has a number of static factory methods for constructing thread pools:

Synchronizers
The java.util.concurrent package contains several classes that help manage a set of collaborating
threads。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值