Java多线程--线程常用操作方法

1、取得和设置线程名称


class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		for(int i=0;i<3;i++){
			System.out.println(Thread.currentThread().getName()
					+ "运行,i = " + i) ;	// 取得当前线程的名字
		}
	}
};
public class ThreadNameDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 实例化Runnable子类对象
		new Thread(mt).start() ;		// 系统自动设置线程名称
		new Thread(mt,"线程-A").start() ;		// 手工设置线程名称
		new Thread(mt,"线程-B").start() ;		// 手工设置线程名称
		new Thread(mt).start() ;		// 系统自动设置线程名称
		new Thread(mt).start() ;		// 系统自动设置线程名称
	}
};

2、取得当前线程 currentThread()

class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		for(int i=0;i<3;i++){
			System.out.println(Thread.currentThread().getName()
					+ "运行,i = " + i) ;	// 取得当前线程的名字
		}
	}
};
public class CurrentThreadDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 实例化Runnable子类对象
		new Thread(mt,"线程").start() ;		// 启动线程
		mt.run() ;	// 直接调用run()方法
	}
};

3、判断线程是否启动  isAlive()

class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		for(int i=0;i<3;i++){
			System.out.println(Thread.currentThread().getName()
					+ "运行,i = " + i) ;	// 取得当前线程的名字
		}
	}
};
public class ThreadAliveDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 实例化Runnable子类对象
		Thread t = new Thread(mt,"线程");		// 实例化Thread对象
		System.out.println("线程开始执行之前 --> " + t.isAlive()) ;	 // 判断是否启动
		t.start() ;	// 启动线程
		System.out.println("线程开始执行之后 --> " + t.isAlive()) ;	 // 判断是否启动
		for(int i=0;i<3;i++){
			System.out.println(" main运行 --> " + i) ;
		}
		// 以下的输出结果不确定
		System.out.println("代码执行之后 --> " + t.isAlive()) ;	 // 判断是否启动
		
	}
};

4、线程的强制执行 join()


class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		for(int i=0;i<50;i++){
			System.out.println(Thread.currentThread().getName()
					+ "运行,i = " + i) ;	// 取得当前线程的名字
		}
	}
};
public class ThreadJoinDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 实例化Runnable子类对象
		Thread t = new Thread(mt,"线程");		// 实例化Thread对象
		t.start() ;	// 启动线程
		for(int i=0;i<50;i++){
			if(i>10){
				try{
					t.join() ;	// 线程强制运行
				}catch(InterruptedException e){}
			}
			System.out.println("Main线程运行 --> " + i) ;
		}
	}
};

5、线程的休眠   sleep()


class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		for(int i=0;i<50;i++){
			try{
					Thread.sleep(500) ;	// 线程休眠
			}catch(InterruptedException e){}
			System.out.println(Thread.currentThread().getName()
					+ "运行,i = " + i) ;	// 取得当前线程的名字
		}
	}
};
public class ThreadSleepDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 实例化Runnable子类对象
		Thread t = new Thread(mt,"线程");		// 实例化Thread对象
		t.start() ;	// 启动线程
	}
};

6、线程的中断   interrupt()


class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		System.out.println("1、进入run()方法") ;
		try{
				Thread.sleep(10000) ;	// 线程休眠10秒
				System.out.println("2、已经完成了休眠") ;
		}catch(InterruptedException e){
			System.out.println("3、休眠被终止") ;
			return ; // 返回调用处
		}
		System.out.println("4、run()方法正常结束") ;
	}
};
public class ThreadInterruptDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 实例化Runnable子类对象
		Thread t = new Thread(mt,"线程");		// 实例化Thread对象
		t.start() ;	// 启动线程
		try{
				Thread.sleep(2000) ;	// 线程休眠2秒
		}catch(InterruptedException e){
			
		}
		t.interrupt() ;	// 中断线程执行
	}
};

7、后台线程 setDaemon()



class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		while(true){
			System.out.println(Thread.currentThread().getName() + "在运行。") ;
		}
	}
};
public class ThreadDaemonDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 实例化Runnable子类对象
		Thread t = new Thread(mt,"线程");		// 实例化Thread对象
		t.setDaemon(true) ;	// 此线程在后台运行
		t.start() ;	// 启动线程
	}
};

8、线程的优先级 setPriority()


class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		for(int i=0;i<5;i++){
			try{
					Thread.sleep(500) ;	// 线程休眠
			}catch(InterruptedException e){}
			System.out.println(Thread.currentThread().getName()
					+ "运行,i = " + i) ;	// 取得当前线程的名字
		}
	}
};
public class ThreadPriorityDemo{
	public static void main(String args[]){
		Thread t1 = new Thread(new MyThread(),"线程A") ;	// 实例化线程对象
		Thread t2 = new Thread(new MyThread(),"线程B") ;	// 实例化线程对象
		Thread t3 = new Thread(new MyThread(),"线程C") ;	// 实例化线程对象
		t1.setPriority(Thread.MIN_PRIORITY) ;	// 优先级最低
		t2.setPriority(Thread.MAX_PRIORITY) ;	// 优先级最高
		t3.setPriority(Thread.NORM_PRIORITY) ;	// 优先级中等
		t1.start() ;	// 启动线程
		t2.start() ;	// 启动线程
		t3.start() ;	// 启动线程
	}
};
public class MainPriorityDemo{
	public static void main(String args[]){
		System.out.println("主方法的优先级:" + 
			Thread.currentThread().getPriority()) ;	// 取得主方法的优先级
		System.out.println("MAX_PRIORITY = " + Thread.MAX_PRIORITY) ;
		System.out.println("NORM_PRIORITY = " + Thread.NORM_PRIORITY) ;
		System.out.println("MIN_PRIORITY = " + Thread.MIN_PRIORITY) ;
	}
};

9、线程的礼让 yield()


class MyThread implements Runnable{	// 实现Runnable接口
	public void run(){	// 覆写run()方法
		for(int i=0;i<5;i++){
			try{
				Thread.sleep(500) ;
			}catch(Exception e){}
			System.out.println(Thread.currentThread().getName()
					+ "运行,i = " + i) ;	// 取得当前线程的名字
			if(i==2){
				System.out.print("线程礼让:") ;
				Thread.currentThread().yield() ;	// 线程礼让
			}
		}
	}
};
public class ThreadYieldDemo{
	public static void main(String args[]){
		MyThread my = new MyThread() ;	// 实例化MyThread对象
		Thread t1 = new Thread(my,"线程A") ;
		Thread t2 = new Thread(my,"线程B") ;
		t1.start() ;
		t2.start() ;
	}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值