Thread(Runnable runnable)
package CSDN.day02;
import java.util.Arrays;
public class _04 {
public static void main(String[] args) {
/**
* 使用Thread(Runnable runnable),创建一个线程对象。
* 任务: 随机5个int数据[0~100],放入数组中,并冒泡排序,输出
*/
Thread t1 = new Thread(()->{
int[] arr = new int[5];
for(int i=0;i<arr.length;i++){
arr[i] = (int)(Math.random()*101);
}
for(int i = 0;i<arr.length-1;i++){
for(int j= 0;j<arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j ]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println(Arrays.toString(arr));
});
}
}
Thread(Runnable runnable,String name)
package CSDN.day02;
public class _05 {
public static void main(String[] args) {
Runnable r = ()->{
String name = Thread.currentThread().getName();
for (int i = 0; i < 10; i++) {
System.out.println(name+"说我喜欢你");
}
};
Thread thread = new Thread(r,"thread-A");
thread.start();
}
}
Thread(String name)
package CSDN.day02;
public class _06 {
public static void main(String[] args) {
Thread t1 = new Thread("thread-B"){
public void run(){
String name = this.getName();
for(int i=0; i<10; i++){
System.out.println(name+"说:i like you "+i);
}
}
};
t1.start();
}
}
线程常用的属性方法
注:main方法:本质上就是一个线程。
public class PropertyMethodDemo {
public static void main(String[] args) {
/**
* main方法:本质上就是一个线程。
*/
//1. 获取当前线程对象
Thread current = Thread.currentThread();
//2. 获取当前线程的名字
String name = current.getName();
//3. 获取当前线程的唯一标识符
long id = current.getId();
//4. 获取当前线程的优先级
int priority = current.getPriority();
//5. 获取当前线程的状态
Thread.State state = current.getState();
//6. 查看当前线程是否存活
boolean alive = current.isAlive();
//7. 查看当前线程是否被打断了
boolean interrupted = current.isInterrupted();
//8. 查看当前线程是否为守护进程
boolean daemon = current.isDaemon();
System.out.println("Current thread: " + name);
System.out.println("ID: " + id);
System.out.println("Priority: " + priority);
System.out.println("State: " + state.name());
System.out.println("Alive: " + alive);
System.out.println("Interrupted: " + interrupted);
System.out.println("Daemon: " + daemon);
}
}
守护线程
1. 进程要么是守护线程,要么前台线程 2. 前台线程: 在表面运行的,能看到的。 或者是daemon的值为false。 3. 后台线程:就是守护线程,即daemon的值为true。
注意: 所有的前台线程都结束了,后台线程就会立即结束。
public class _08 {
public static void main(String[] args) {
Thread rose = new Thread("rose"){
public void run(){
for(int i=1;i<=5;i++){
System.out.println(getName()+
"说:Ijump");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("----我落水了------");
}
};
Thread jack = new Thread("jack"){
public void run(){
for(int i=1;i<=5;i++){
System.out.println(getName()+
"说:you jump,Ijump");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
jack.setDaemon(true);
rose.start();
jack.start();
}
}
线程的睡眠方法
static void sleep(long time)
解析: 线程的睡眠方法,可以让当前线程进入休眠状态,进入阻塞状态,不再占用cpu的使用时间。 时间单位是毫秒。休眠时间一过,立马进入就绪状态(等待调度器分配时间片段) 注意:休眠时间内,可能会被异常中断。因此要处理异常InterruptedException static void sleep(long time,int nanos) 两个参数的方法: time:单位是毫秒 nanos: 单位是纳秒
package CSDN.day02;
public class _09 {
public static void main(String[] args) {
Thread t1 = new Thread("download"){
public void run(){
for (int i = 1; i <= 10; i++) {
System.out.println("正在下载视频中······"+(i*10)+"%");
//使用休眠方法,来假装模拟正在下载中
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t1.setDaemon(true);
t1.start();
}
}
线程的礼让方法
线程的礼让方法,表示让出cpu的使用权,会进入就绪状态。 不过,下一个时间片段还可能是该线程的。
package CSDN.day02;
public class _10 {
public static void main(String[] args) {
Thread t1 = new Thread("Thread-1"){
public void run(){
for(int i=1;i<=10;i++){
System.out.println(getName()+" "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
Thread t2 = new Thread("Thread-2"){
public void run(){
for(int i=1;i<=10;i++){
if(i==5){
Thread.yield();
}
System.out.println(getName()+" "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
t1.start();
t2.start();
}
}
线程的加入方法
解析: 另一个线程加入方法。此时当前线程就会处于阻塞状态。另一个线程结束后,当前线程 会进入就绪状态。 案例演示: 图片下载线程: 图片显示线程: 过程: 先下载,然后再显示
package CSDN.day02;
public class _11 {
public static void main(String[] args) {
Thread download = new Thread("download"){
public void run() {
for (int i = 1; i < 101; i++) {
System.out.println(getName()+":图片正在下载中····进度条:"+i+"%");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("图片下载完成...");
}
};
Thread show = new Thread("show"){
public void run() {
try {
download.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
for (int i = 1; i < 101; i++) {
System.out.println(getName()+":"+i+"%");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("图片显示完成");
}
};
download.start();
show.start();
}
}
线程的打断方法
解析: 线程中断、打断方法。 表示当前线程去打断另外一个线程。打断哪个线程就用那个线程调用 注意: 是在当前线程中调用另一个线程的打断方法。
package CSDN.day02;
public class _12 {
public static void main(String[] args) {
Thread lin = new Thread("林永健"){
public void run() {
System.out.println(getName()+"说:我正在睡觉");
try {
//模拟正在睡觉
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(getName()+"说:干啥?都破了相了");
}
}
};
Thread huang = new Thread("黄宏"){
public void run() {
for (int i = 0; i < 9; i++) {
System.out.println(getName()+"说:"+(i+1)+"个80");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println(getName()+"说:搞定了!");
lin.interrupt();
}
};
lin.start();
huang.start();
}
}