1.线程方法:A:线程加入:public final void join(),等待该线程终止,其他线程才能继续执行。
B:线程礼让:public static void yield(),暂停正在执行的线程,让线程的执行更和谐一些。
C:线程死亡:public final void stop(),public void interrupt()
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyThread extends Thread {
public void run(){
System.out.println(new SimpleDateFormat("HH-mm-ss").format(new Date()));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("long may the sunshine!"+getName());
}
System.out.println(new SimpleDateFormat("HH-mm-ss").format(new Date()));
}
}
public class Test {
public static void main(String[] args) {
MyThread th1=new MyThread();
th1.setName("a");
th1.start();
try {
Thread.sleep(3000);
th1.interrupt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
先执行interrupt,结果是在线程被杀死后,还可以执行完毕线程中的代码,打印出开始和结束的时间若把程序中的
th1.interrupt();
更改为
th1.stop();
则在线程开启到被杀死时只会执行被杀死之前的代码,也就是只会输出开始时间而不能输出全部内容。
D:线程休眠:static void sleep(long millis),线程睡眠输入的毫秒值。
2.线程的生命周期:新建,就绪,运行,阻塞(有可能),死亡
3.线程间通信:不同线程针对同一个资源的操作。
线程不安全现象及原因:A:输出线程没有获取到数据,输出为空,由于两个线程操作的不是同一个对象,因此输出
无法对应输入。应该用构造传参将相同的操作对象传入两个线程中。
B:操作对象传入后,发现输出的数据出现错乱,这是因为多个线程操作相同的数据,出现
线程安全问题,此时需要加锁解决线程安全问题。
C:加上锁之后,仍会出现线程安全问题,即数据不对应,这是因为需要加上同一把锁,既
需要一个共同的锁对象,因此把锁对象均设置为操作对象的类。
D:加上锁之后,数据不错乱,线程安全问题解决,此时要想自行调节输入线程和输出线程
的运行规则,就要利用线程的等待唤醒机制,使线程有规律的运行而不是盲目抢占。
针对上述方案对输入输出进行代码修改:
public class Student {
private String name;
private int age;
private boolean flag;//用来判断下一步操作的标志true代表有数据,false代表无数据
public synchronized void SetIn(String name,int age){
if(this.flag){//若有数据,不进行赋值,让输入线程等待,输出线程占用资源
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name=name;//若没有数据,进行赋值,并改变标志
this.age=age;
this.flag=true;
this.notify();//待线程执行结束,唤醒所有线程,进行下一步判断
}
public synchronized void GetIn(){
if(!this.flag){//如果没有数据,输出线程等待
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//有数据就输出并改变标志位
System.out.println(this.name+" "+this.age);
this.flag=false;
this.notify();//输出结束后唤醒所有线程
}
}
public class SetThread extends Thread{
private Student s;
private int x=0;
public SetThread(Student s) {
super();
this.s = s;
}
public void run(){
while(true){
if(x%2==0){
this.s.SetIn("a", 4);
}else{
this.s.SetIn("b", 5);
}
x++;
}
}
}
public class GetThread extends Thread{
private Student s;
public GetThread(Student s) {//构造传参
super();
this.s = s;
}
public void run(){
while(true){
this.s.GetIn();//在java类中直接给出操作方法
}
}
}
测试类:
public class Test {
public static void main(String[] args) {
Student s=new Student();
SetThread st=new SetThread(s);
GetThread gt=new GetThread(s);
st.start();
gt.start();
}
}
4.线程组:ThreadGroup表示,可以对一批线程进行分类管理,java允许程序直接对线程组进行控制。默认情况下,
所有线程都属于主线程组。
方法:public final ThreadGroup getThreadGroup():获取线程对应的线程组对象
Thread(ThreadGroup group, Runnable target),给线程设置分组。
案例2:创建线程组对象,给线程分配线程组
public class MyThread extends Thread{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
public class Test {
public static void main(String[] args) {
MyThread th1=new MyThread();
ThreadGroup t1=th1.getThreadGroup();
System.out.println(t1.getName());
System.out.println("---------------------");
ThreadGroup t2=new ThreadGroup("prise sunshine");
Thread th2=new Thread(t2, th1);
System.out.println(th1.getName());
System.out.println(th2.getThreadGroup());
}
}
5.线程池:当程序中要创建大量生存周期很短的线程时,应该考虑使用线程池,线程池中的线程代码结束后,县城不
会死亡,而是回到线程池中等待下一个对象来使用。
6.线程池使用步骤:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) {
//1.创建线程池对象
ExecutorService pool=Executors.newFixedThreadPool(3);
//2.创建线程/rannable实例
MyCallable m1=new MyCallable();
//3.向线程池中提交任务
pool.submit(m1);
//4.关闭线程池
pool.shutdown();
}
}
7.定时器:Timer:构造方法:public Timer()
成员方法:A:public void schedule(TimerTask task, long delay)延迟多久执行任务
B:public void schedule(TimerTask task,long delay,long period)延迟多久执行任务,并以后每隔
多久执行一次
C:public boolean cancel()取消这个任务
D:schedule(TimerTask task, Date time) 安排在指定的时间执行指定的任务