获取当前线程
Thread下的静态方法
static Thread currentThread(); 获取当前的线程
成员方法:
void setName(String s); //更改当前线程的名字
String getName(); //获取线程的名字
public class Test {
public static void main(String[] args) {
Thread t1 = new Thread(new Processor());
//给线程起名
t1.setName("t1");
t1.start();
Thread t2 = new Thread(new Processor());
//给线程起名
t2.setName("t2");
t2.start();
Thread t= Thread.currentThread(); //t保存的内存地址指向的线程是“主线程对象”
t.setName("mainnnn");
//获取线程的名字
System.out.println(t.getName());
}
}
class Processor implements Runnable{
public void run() {
Thread t= Thread.currentThread(); //t保存的内存地址指向的线程是“t1线程对象”
System.out.println(t.getName());
}
}
线程优先级
分时调度模型:所有线程轮流使用CPU的使用权,平均分配每个线程占用的时间片
抢占式调度模型:优先让优先级高的线程使用CPU,如果线程的优先级相同,那么会随机选择一个,优先级高的线程获取的CPU时间片相对多一些
JVM使用的是抢占式调度模型
线程优先级主要分三种:MAX_PRIORITY(最高级 10),MIN_PRIORITY(最低级 1),NORM_PRIORITY(标准 5)(默认) 总共分10个等级
public class Test2 {
public static void main(String[] args) {
//定义线程
Thread t1 = new Thread(new Processors());
t1.setName("t1");
Thread t2 = new Thread(new Processors());
t2.setName("t2");
System.out.println(t2.getPriority());
System.out.println(t1.getPriority());
//设置优先级
t1.setPriority(10);
t2.setPriority(1);
t1.start();
t2.start();
}
}
class Processors implements Runnable
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(Thread.currentThread().getName()+"---->"+i);
}
}
}
线程调度
1.static void sleep(long millis) 使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行),具体取决于系统定时器和调度程序的精度和准确性。
Thread.sleep(毫秒)
作用:阻塞当前线程,腾出CPU,让给其他线程
public class SleepTest {
public static void main(String[] args) {
Thread t1 = new Thread(new Processor());
t1.start();
t1.sleep(1000); 这里用对象名去调用静态名,相当于是用类名调用静态方法是一样的,等同于Thread.sleep(1000) //相当于主线程休眠与t1没有关系
for(int i=0;i<10;i++)
{
System.out.println(Thread.currentThread().getName());
}
}
}
class Processor extends Thread{
//对于run内部方法发生异常不能直接上抛,因为父类中的run方法是没有抛出异常的
//子类中抛出的异常不能比父类中更加广泛
//run中的方法只能通过try..catch处理
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(Thread.currentThread().getName()+"--->"+i);
try {
Thread.sleep(1); //这里注意sleep需要抛出异常
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
某线程处在休眠状态时,如何打断它的休眠
成员方法:
public void interrupt()
public class SleepTest02 {
public static void main(String[] args) throws Exception{
//需求:启动线程,5s之后打断线程的休眠
Thread t = new Thread(new Processorse());
t.setName("s");
t.start();
Thread.sleep(5000);
//打断休眠
t.interrupt(); //打断了休眠后,线程里面的sleep方法会报错异常被try...catch捕获
}
}
class Processorse implements Runnable
{
public void run() {
try {
Thread.sleep(1000000000);
}catch(Exception e) {
e.printStackTrace();
}
for(int i=0;i<10;i++) {
System.out.println(Thread.currentThread().getName()+"------>"+i);
}
}
}
如何正确的更好的终止一个正在执行的线程
需求:线程启动5s之后终止
在线程类内部定义一个暂停的标志run,以及注意下一些具体的写法
通过实例化线程类,来改变内部的属性
//需求 5s之后终止
public class SleepTest03 {
public static void main(String[] args) throws Exception{
Proeceeewe p1 = new Proeceeewe();
Thread t1 = new Thread(p1);
t1.start();
//5s之后
Thread.sleep(5000);
p1.run = false;
}
}
class Proeceeewe implements Runnable
{
boolean run = true;
public void run() {
for(int i=0;i<10;i++)
{
if(run == true)
{ try {
Thread.sleep(1000);
System.out.println("----->"+i);
}catch(Exception e) {
e.printStackTrace();
}
}else
{
return;
}
}
}
}
yield方法
静态方法yield
作用:给同一个优先级的线程让位。但是让位时间不固定
和sleep方法相同,就是yield让出的时间不固定,不能由自定义
线程的合并join方法
成员方法:join()
public class JoinTest {
public static void main(String[] args) throws Exception {
Thread t =new Thread(new Proeceeewes());
t.setName("t1");
t.start();
//合并线程
t.join(); //join 在哪里出现,就是当前线程和前面的线程名对应的线程合并 //这里是主线程和t线程合并
System.out.println(Thread.currentThread().getName());
}
}
class Proeceeewes implements Runnable
{
public void run() {
for(int i=0;i<10;i++)
{
try {
Thread.sleep(1000);
System.out.println("----->"+i);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}