1.线程的两种实现方法:
(1)继承Thread类
PasswordHandle.java
package thread;
public class PasswordHandle extends Thread {
private boolean interruptFlag;
private String mask;
public PasswordHandle() {
this('*');
}
public PasswordHandle(char maskchar) {
this.interruptFlag = true;
mask = maskchar+"";
}
public void setInterruptFlag(boolean interruptFlag) {
this.interruptFlag = interruptFlag;
}
@Override
public void run() {
while (true) {
if(!interruptFlag) break;
System.out.print(mask);
try {
Thread.currentThread().sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
pwsThreadTest.java
package thread;
import java.util.Scanner;
public class pwsThreadTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("name:");
String name = scanner.next();
System.out.print("password:");
PasswordHandle pthread = new PasswordHandle();
pthread.start();
String pws = scanner.next();
pthread.setInterruptFlag(false);
if ("suky".equals(name) && "qwe123".equals(pws)) {
System.out.println("Welcome " + name +"!");
break;
} else {
System.out.println("The name or password is error,please try again!");
}
}
}
}
(2)继承Runnable接口
修改PasswordHandle.java 继承接口Runnable。启动线程部分改成
PasswordHandle handle = new PasswordHandle();
Thread pthread = new Thread(handle);
pthread.start();
2.main方法开始至少会启动两个线程,一个主线程,还有一个是垃圾收集器的线程,后者是一个非Deamon线程,不会随着主线程的结束而停止工作。当我们希望一个线程在产生它的线程结束后停止,可以通过setDaemon(true)方法来设置,使之成为Deamon线程。
示例:
package thread;
public class DeamonTest {
/**
* @param args
*/
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
public void run() {
while (true) {
System.out.print("B");
}
}
});
thread.setDaemon(true);// 设定为Deamon线程
thread.start();
for(int i=0;i<5;i++){
System.out.print("A");
}
}
}
3.线程生命周期:创建线程,可执行,阻塞,终止
- start():线程进入可执行状态,执行run方法
- yield():让出执行权,加入线程的队列,等待
- sleep()/wait():让线程处于阻塞状态暂停一段时间,不同在于wait会释放当前线程占有 资源的锁,而sleep不会。所以wait()一般出现在synchronized范畴内
synchronized(this) {
this.wait();
}
- notify()/notifyAll():重新把锁还给阻塞的线程重而使其继续执行。
4.线程优先权
1 (Thread.MIN_PRIORITY)
10(Thread.MAX_PRIORITY)
默认:5(Thread.NORM_PRIORITY),手动设置调用setPriority方法
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);
}
}
5.InterruptedException异常
当使用sleep(),在暂停的时间内中断的话,会抛出此异常。示例:
package thread;
public class InterruptTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Thread A start...");
Thread threadB = new Thread(new Runnable(){
public void run(){
try{
System.out.println("Thread B start...");
for(int i=0;i<5;i++){
Thread.sleep(1000);
System.out.println("Thread B execute...");
}
System.out.println("Thread B end...");
}catch(InterruptedException e){e.printStackTrace();}
}
});
threadB.start();
threadB.interrupt();//立即中断
}
}
输出:
Thread A start...
Thread B start...
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at thread.InterruptTest$1.run(InterruptTest.java:16)
at java.lang.Thread.run(Thread.java:619)
6.join():线程B加入线程A,A等待B完成后,继续A。
7.线程停止: sleep,wait方法,可以使用interrupt();I/O则必须等到输入/输出动作结束,替代的方法可以是抛出异常,离开 run()。