自学java多线程笔记(一)

1.线程的3种实现方式

package com.test.web.controller;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestThread {
	public static void main(String[] args) {
		//继承Thread类
		MyThread myThread = new MyThread();
		myThread.start();
		
		//实现runnable接口
		MyRunnable myRunnable = new MyRunnable();
		Thread thread = new Thread(myRunnable);
		thread.start();
		
		//实现Callable接口
		MyCallable myCallable = new MyCallable();
		FutureTask<Integer>futureTask = new FutureTask<Integer>(myCallable);
		Thread t = new Thread(futureTask);
		t.start();
		
		try {
			int sum = futureTask.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
}

class MyThread extends Thread{
	@Override
	public void run(){
		
	}
}

class MyRunnable implements Runnable{

	@Override
	public void run() {
		
	}
}

class MyCallable implements Callable<Integer>{

	@Override
	public Integer call() throws Exception {
		
		return null;
	}
	
}

2.线程的状态转换


3.线程的基本控制方法

方法功能
isAlive()判断是线程是否终止
getPriority()得到线程的优先级数值
setPriority()设置线程的优先级数值
Thread.sleep()将当前线程置顶睡眠的毫秒数
join()调用某线程的join方法,将当前线程与该线程合并,即等待该线程结束,再回复当前线程的运行
wait()当前线程进入对象的wait pool 
yield()让出cpu,让当前线程进入就绪队列等待调度
notify()/notifyAll()唤醒对象的wait pool中的一个/所有的等待线程

4.方法示例

    1.sleep()方法

public class TestThread {
	public static void main(String[] args) {
		//继承Thread类
		MyThread myThread = new MyThread();
		myThread.start();
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		myThread.interrupt();
	}
}

class MyThread extends Thread{
	@Override
	public void run(){
		while(true){
			System.out.println("==="+new Date()+"===");
			try {
				Thread.sleep(1000);//这里只能写try catch来捕获异常,子类不能抛出和父类不同的异常
			} catch (InterruptedException e) {
				return;
			}
		}
	}
}
}

   sleep()方法只要被打断,就一定会抛出异常,但是interrupt()这个方法比较粗暴,当线程正在打开一个文件流的时候,调用interrupt()方法,结束线程,那么当前的文件流就会永远的打开。下面的方法就优化了上面方法的缺陷:定义一个私有的变量,和一个public方法,调用这个public方法来终止线程的运行。

public class TestThread {
	public static void main(String[] args) {
		//继承Thread类
		MyThread myThread = new MyThread();
		myThread.start();
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		myThread.stopSleep();
	}
}

class MyThread extends Thread{
	private boolean flag = true;
	@Override
	public void run(){
		while(flag){
			System.out.println("==="+new Date()+"===");
			try {
				Thread.sleep(1000);//这里只能写try catch来捕获异常,子类不能抛出和父类不同的异常
			} catch (InterruptedException e) {
				return;
			}
		}
	}
	public void stopSleep(){
		flag = false;
	}
}	
}

2.join方法

public class TestThread {
	public static void main(String[] args) {
		//继承Thread类
		MyThread myThread = new MyThread("abcd");
		myThread.start();
		try {
			myThread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		for(int i =0;i<=10;i++){
			System.out.println("i am main Thread");
		}
	}
}

class MyThread extends Thread{
	MyThread(String s){
		super(s);
	}
	@Override
	public void run(){
		for(int i =0;i<=10;i++){
			System.out.println("i am "+ getName());
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

3.yield()方法

    yield()方法比较高风亮节,当线程调用 yield()方法时候,就会让出当前的时间片,给其他的线程来执行

public class TestThread {
	public static void main(String[] args) {
		//继承Thread类
		MyThread myThread1 = new MyThread("t1");
		MyThread myThread2 = new MyThread("t2");
		myThread1.start();
		myThread2.start();
	}
}

class MyThread extends Thread{
	MyThread(String s){
		super(s);
	}
	@Override
	public void run(){
		for(int i =0;i<=100;i++){
			System.out.println(getName()+":"+i);
			if(i%10 == 0){
				yield();
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值