我不得不说,每次都是任务到跟前了,才不得不迅速的去搞明白某个东西。。。
这种状态真不好。。。
多进程与多线程本质的区别在于每个进程拥有自己的一整套变量。而线程则共享数据。
一个最简单的使用线程的例子。
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。