进程的退出:
代码:
public class ThreadExit {
public static void main(String[] args) throws InterruptedException {
Tag tag = new Tag();
new Thread(tag).start();
Thread.sleep(10*1000);//main线程休眠10s
tag.setLoop(false);//让线程Thread-0退出
}
}
class Tag implements Runnable{
private Boolean loop = true;
public Boolean getLoop() {
return loop;
}
public void setLoop(Boolean loop) {
this.loop = loop;
}
@Override
public void run() {//这个run()并非是Thread类中的方法,而是Thread类实现接口Runnable的方法:public abstract void run();
while (loop){
System.out.println("冲啊"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
常用方法:
- setName()设置线程的名字
- setPriority()设置线程的优先级
- start()开始线程
- interrupt()中断线程
- getName()得到线程的名字
- join()线程插队,是要插队的线程的实例对象来调用它
- yeild()线程礼让,是该线程来调用它,如Thread.yeild();
代码:
public class ThreadMethods1 {
public static void main(String[] args) throws InterruptedException {
Tag tag = new Tag();
Thread thread = new Thread(tag);
thread.setName("ret");//setName()设置线程的名字
thread.setPriority(Thread.MIN_PRIORITY);//setPriority()设置线程的优先级MIN_PRIORITY为1
thread.start();//start()开始线程
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "正在学JavaWeb");
}
thread.interrupt();//interrupt()中断线程。这里执行完后main线程就结束了,后面不会在中断ret线程了
}
}
class Tag implements Runnable {
@Override
public void run() {
while (true) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "正在学Java。 优先级为"+Thread.currentThread().getPriority());//getName()得到线程的名字
}
try {
System.out.println(Thread.currentThread().getName()+"休眠中");
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
System.out.println("你已经被中断了,继续~");
}
}
}
}
public class ThreadMethods2 {
public static void main(String[] args) throws InterruptedException {
Person person = new Person();
Thread thread = new Thread(person);
thread.setName("ret");
thread.start();
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "正在学JavaWeb");
Thread.sleep(1000);
if (i == 5) {
System.out.println(thread.getName() + "先学");
thread.join();//join()线程插队,让线程名为thread.getName()的先执行完,即线程Person
//Thread.yield();//yield()线程礼让,让其余线程先执行
}
}
}
}
class Person implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "正在学Java");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
工作线程(用户线程):当线程的任务执行完或通知方式结束
守护线程:一般是为工作线程服务的,当所有的工作线程结束,守护线程自动结束
常见的守护线程:垃圾回收机制
代码:
实例对象.setDaemon(true);//将线程设置为守护线程
public class Daemon {
public static void main(String[] args) throws InterruptedException {
Tag tag = new Tag();
Thread thread = new Thread(tag);
thread.setDaemon(true);//将线程Tag设置为守护线程
thread.start();
for (int i = 0; i < 5; i++) {
System.out.println("ret");
Thread.sleep(1000);
}
}
}
class Tag implements Runnable{
@Override
public void run() {//这个run()并非是Thread类中的方法,而是Thread类实现接口Runnable的方法:public abstract void run();
while (true){
System.out.println("冲啊"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}