java线程通信

我理解的通信就是数据信息之间的互相传递.那么Thread之间的通信也无法跳出这个定义.

在Thread之间通信只要采用回调的方式.

那么什么是回调了?举个简单的例子:A把作业写完后告诉B.其实B有很多种方式可以获得A的状态.1.定时检测A,查看A的状态.但是会造成资源的浪费.也就是B要是不是的停下来询问A是否完成作业.2.让A在适当的时候主动的将状态回传给B.方式2就采用了回调机制.

1.静态回调

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Teacher {
	public static void getStudentStatus(String status){
		//you can do something here
		System.out.println(status);
		//you can do something here
	}
	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		service.execute(new Thread(new StudentThread(true)));
		service.execute(new Thread(new StudentThread(false)));
		service.shutdown();
	}
}
public class StudentThread implements Runnable {
	private boolean isPlay;
	public StudentThread(boolean isPlay) {
		this.isPlay = isPlay;
	}
	@Override
	public void run() {
		if(isPlay) {
			//you can do something here
			Teacher.getStudentStatus("rest");
		} else {
			//you can do something here
			Teacher.getStudentStatus("inclass");
		}
	}
}

优点:不用实例化对象可以直接应用,如果是变量可以直接赋值

缺点:

1.在初始化时被加载,如果后续不被使用增加内存的负荷影响效率(一般很小).如果是变量多处使用的话导致数据混乱.增加维护成本.

2.无法享受面向对象带来的特性


2.实例回调

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Teacher {

	private String type;
	
	public Teacher(String type) {
		this.type = type;
	}
	
	public void getStudentStatus(String status){
		//you can do something here
		System.out.println("student status:" + status + " , teacher type " + type);
		//you can do something here
	}
	
	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		service.execute(new Thread(new StudentThread(true, new Teacher("Sports"))));
		service.execute(new Thread(new StudentThread(false, new Teacher("math"))));
		service.shutdown();
	}
}
public class StudentThread implements Runnable {
	
	private boolean isPlay;
	
	private Teacher teacher;
	
	public StudentThread(boolean isPlay,Teacher teacher) {
		this.isPlay = isPlay;
		this.teacher = teacher;
	}


	@Override
	public void run() {
		if(isPlay) {
			//you can do something here
			teacher.getStudentStatus("rest");
		} else {
			//you can do something here
			teacher.getStudentStatus("inclass");
		}
	}


}
//output:
student status:inclass , teacher type math
student status:rest , teacher type Sports

 

优点:

1.可以在其他线程触发该实例的相关动作.也就是可以操作该实例

缺点:

1.其他线程持有特定的对象,不方便后续的扩展

3.接口回调

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Teacher implements Callback{
	private String type;
	public Teacher(String type) {
		this.type = type;
	}
	@Override
	public void callback(String status) {
		//you can do something here
		System.out.println("student status:" + status + " , teacher type " + type);
		//you can do something here
	}
	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		service.execute(new Thread(new StudentThread(true, new Teacher("Sports"))));
		service.execute(new Thread(new StudentThread(false, new Teacher("math"))));
		service.shutdown();
	}
}
public class StudentThread implements Runnable {
	private boolean isPlay;
	private Callback teacherCallback;
	public StudentThread(boolean isPlay,Callback teacherCallback) {
		this.isPlay = isPlay;
		this.teacherCallback = teacherCallback;
	}
	@Override
	public void run() {
		if(isPlay) {
			//you can do something here
			teacherCallback.callback("rest");
		} else {
			//you can do something here
			teacherCallback.callback("inclass");
		}
	}
}
public interface Callback {
	public void callback(String status);
}
//output
student status:rest , teacher type Sports
student status:inclass , teacher type math

优点:

1.可以在其他线程触发该实例的相关动作.也就是可以操作该实例

2.其他线程持有的是接口对象,那么凡是接受该接口参数的线程都可处理该接口的实现

缺点:NA

接口回调是能够发挥面向对象特性最大优势的回调方式


4.实现Callable接口

package callable;

import java.util.concurrent.Callable;

public class StudentThread implements Callable<String> {
	
	private boolean isPlay;
	
	public StudentThread(boolean isPlay) {
		this.isPlay = isPlay;
	}
	
	@Override
	public String call() throws Exception {
		if(isPlay) {
			//you can do something here
			return "rest";
		} else {
			//you can do something here
			return "inclass";
		}
	}

}
package callable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Teacher {

	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		Future<String> future = service.submit(new StudentThread(true));
		try {
			System.out.println("stutent status: " + future.get());
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}finally {
			service.shutdown();
		}
	}
}
//output
stutent status: rest
这种方式的优缺点暂时还不知道.后续研究后在更新

有关各种回调的优缺点希望大家继续补充.谢谢!



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值