1.进程、线程、多线程的区别
进程:
线程:
多线程:
2.使用Java中的线程
- 1.继承java.lang.Thread类
- 2.实现java.lang.Runnable接口
2.1自定义继承Thread类
public class CustomerThread extends Thread {
@Override
public void run() {
super.run();
System.out.println("customer thread!");
}
}
public class Client {
public static void main(String[] args) {
CustomerThread customerThread = new CustomerThread();
customerThread.start();
System.out.println("main 函数");
}
}
2.2自定义继承Runnable接口
public class CustomerRunable implements Runnable {
@Override
public void run() {
System.out.println("customer runable.");
}
}
public class Client {
public static void main(String[] args) {
CustomerRunable customerRunable = new CustomerRunable();
Thread thread = new Thread(customerRunable);
thread.start();
System.out.println("main 函数");
}
}
3.实例变量与线程安全
3.1不共享线程实例变量
public class MyThread extends Thread {
private int count = 5;
public MyThread(String name) {
super();
this.setName(name);
}
@Override
public void run() {
super.run();
while (count > 0) {
count--;
System.out.println("由" + this.currentThread().getName() + "计算,count=" + count);
}
}
}
public class Client {
public static void main(String[] args) {
MyThread a = new MyThread("a");
MyThread b = new MyThread("b");
MyThread c = new MyThread("c");
a.start();
b.start();
c.start();
}
}
3.2共享线程实例变量
public class MyThread extends Thread {
private int count = 5;
public MyThread(String name) {
super();
this.setName(name);
}
@Override
public void run() {
super.run();
count--;
System.out.println("由" + this.currentThread().getName() + "计算,count=" + count);
}
}
public class Client {
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread a = new Thread(myThread, "a");
Thread b = new Thread(myThread, "b");
Thread c = new Thread(myThread, "c");
Thread d = new Thread(myThread, "d");
a.start();
b.start();
c.start();
d.start();
}
}
3.3线程安全
4.currentThread
5.isAlive
6.sleep
7.getId
8.停止线程
8.1判断线程是否停止状态
- 1.静态方法interrupted()
/**
* Tests whether the current thread has been interrupted. The
* <i>interrupted status</i> of the thread is cleared by this method. In
* other words, if this method were to be called twice in succession, the
* second call would return false (unless the current thread were
* interrupted again, after the first call had cleared its interrupted
* status and before the second call had examined it).
*
* <p>A thread interruption ignored because a thread was not alive
* at the time of the interrupt will be reflected by this method
* returning false.
*
* @return <code>true</code> if the current thread has been interrupted;
* <code>false</code> otherwise.
* @see #isInterrupted()
* @revised 6.0
*/
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
- 2.实例方法isInterrupted()
public boolean isInterrupted() {
return isInterrupted(false);
}
8.2代码演示
public class CustomerThread extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 100; i++) {
if (this.isInterrupted()) {
System.out.println("已接收到停止命令!");
break;
}
System.out.println(String.format("i=%s", i));
}
System.out.println("哈哈,我还在...");
}
}
public class Client {
public static void main(String[] args) throws InterruptedException {
CustomerThread thread = new CustomerThread();
thread.start();
Thread.sleep(20);
thread.interrupt();
System.out.println(thread.isInterrupted());
System.out.println("main函数执行完毕!");
}
}
...
i=48
已接收到停止命令!
哈哈,我还在...
true
main函数执行完毕!
- try-catch方式
public class CustomerThread extends Thread {
@Override
public void run() {
super.run();
try{
for (int i = 0; i < 100; i++) {
if (this.isInterrupted()) {
System.out.println("已接收到停止命令!");
throw new InterruptedException();
}
System.out.println(String.format("i=%s", i));
}
System.out.println("哈哈,我还在...");
}
catch (InterruptedException error){
System.out.println(error.getMessage());
error.printStackTrace();
}
}
}
- return方式
@Override
public void run() {
super.run();
for (int i = 0; i < 100; i++) {
if (this.isInterrupted()) {
System.out.println("已接收到停止命令!");
return;
}
System.out.println(String.format("i=%s", i));
}
System.out.println("哈哈,我还在...");
}
9.yield
Thread.yield();
10.线程的优先级
public final void setPriority(int newPriority) {
ThreadGroup g;
checkAccess();
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
setPriority0(priority = newPriority);
}
}