当前位置:网站首页>创建线程,线程状态转换,join,yield,stop,interrupt,wait方法
创建线程,线程状态转换,join,yield,stop,interrupt,wait方法
2022-08-06 05:24:00 【半世晨晓1128】
1.创建线程,继承Thread
package java11.Thread;
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
//休眠
sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package java11.Thread;
public class Test01 {
public static void main(String[] args) {
//创建线程对象
MyThread m = new MyThread();
MyThread m1 = new MyThread();
//启动线程
m.start();
m1.start();
//赋值
m.setName("x");
m1.setName("y");
}
}
2.实现Runnable
package java11.Runnable;
public class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
package java11.Runnable;
public class Test01 {
public static void main(String[] args) {
//创建线程对象
MyThread m = new MyThread();
Thread t = new Thread(m);
Thread t1 = new Thread(m);
//启动线程
t.start();
t1.start();
//赋值
t.setName("x");
t1.setName("y");
}
}
3.实现Callable
package java11.callable;
import java.util.concurrent.Callable;
public class MyThread implements Callable<Integer> {
@Override
public Integer call() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
return sum;
}
}
package java11.callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Test01 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyThread m = new MyThread();
FutureTask<Integer> f = new FutureTask(m);
Thread t = new Thread(f);
t.start();
int n = f.get();
System.out.println("结束:" + n);
}
}
4.线程的状态转换
5.线程的调度
- join():等待其他线程结束
package java11.join;
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
//休眠
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package java11.join;
public class Test01 {
public static void main(String[] args) throws InterruptedException {
//创建线程对象
MyThread m = new MyThread();
//启动线程
m.start();
//赋值
m.setName("x");
//join():等待其他线程结束
m.join();
System.out.println("结束");
}
}
- yield():线程让步,执行一次之后重新抢占cpu.
package java11.yield;
import static java.lang.Thread.yield;
public class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
//线程让步,执行一次之后重新抢占cpu.
yield();
}
}
}
package java11.yield;
import java11.Runnable.MyThread;
public class Test01 {
public static void main(String[] args) {
//创建线程对象
java11.Runnable.MyThread m = new MyThread();
Thread t = new Thread(m);
Thread t1 = new Thread(m);
//启动线程
t.start();
t1.start();
//赋值
t.setName("x");
t1.setName("y");
}
}
- 调整各个线程的优先级
package java11.Runnable;
public class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
package java11.Runnable;
public class Test01 {
public static void main(String[] args) {
//创建线程对象
MyThread m = new MyThread();
Thread t = new Thread(m);
Thread t1 = new Thread(m);
//启动线程
t.start();
t1.start();
//赋值
t.setName("x");
t1.setName("y");
//设置优先级,线程默认的优先级为5。MAX_PRIORITY:取值为10,表示最高优先级。MIN_PRIORITY:取值为1,表示最低优先级
t.setPriority(1);
t1.setPriority(10);
}
}
- stop():停止
package java11.stop;
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(i);
try {
//休眠
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package java11.stop;
import static java.lang.Thread.sleep;
public class Test01 {
public static void main(String[] args) throws InterruptedException {
//创建线程对象
MyThread m = new MyThread();
//启动线程
m.start();
sleep(5000);
System.out.println("结束");
//停止
m.stop();
}
}
- interrupt():中断线程
package java11.interrupt;
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; ; i++) {
System.out.println(i);
if (this.isInterrupted()) {
break;
}
}
}
}
package java11.interrupt;
import static java.lang.Thread.sleep;
public class Test01 {
public static void main(String[] args) throws InterruptedException {
//创建线程对象
MyThread m = new MyThread();
//启动线程
m.start();
sleep(2000);
System.out.println("结束");
//中断
m.interrupt();
}
}
- 有sleep的中断
package java11.interrupt;
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; ; i++) {
System.out.println(i);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
if (this.isInterrupted()) {
break;
}
}
}
}
- wait
package java11.wait;
public class MyThread extends Thread {
boolean flag = false;
@Override
public void run() {
for (int i = 0; ; i++) {
System.out.println(Thread.currentThread().getName() + i);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (flag) {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
package java11.wait;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
MyThread m = new MyThread();
m.start();
Scanner s = new Scanner(System.in);
while (true) {
int n = s.nextInt();
if (n == 1) {
m.flag = true;
} else if (n == 2) {
m.flag = false;
//唤醒
synchronized (m) {
m.notify();
}
} else {
m.stop();
break;
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
5.3 端口扫描:Zenmap工具的应用
分布式,微服务,集群概念和区别
ARMv7-M Debug Part
3.6 Meterpreter 键盘记录
Fortinet :《2021 年OT与网络安全现状报告》之「要点综述」
HAProxy 生产环境架构
在VMware上面安装Solaris 10
无需拆机,Kindle 全系列 5.12.2.2 ~ 5.14.2版本如何越狱?如何安装第三方插件
动态规划之最长连续递增序列
After the Xiaomi AX3600 router is expanded and flashed OpenWrt, how to flash back to the official system and partition
lvs-dr
利用Metasploit控制目标电脑(Win7_x64系统)
二叉搜索树BST
“ALL in Web3”!Web3到底是什么?我们真的需要Web3吗?
redis-(error) CLUSTERDOWN Hash slot not served
学好免交互expect解放双手
MySql 数据表结构优化总结
动态规划之数字n二进制位中1的个数
ARM Cortex-M debugging
4.5 远程入侵









