线程的生命周期包含五个状态
新建状态,就绪状态,运行状态,阻塞状态,终止状态
线程优先级
线程优先级用1~10表示,1表示最高级,默认值是5。每个优先级值对应Thread类中的一个公用静态常量。
与线程优先级有关的方法有两个:
public final int getPriority() //获得线程的优先级
public final void setPriority(int newPriority) //设定线程的优先级
线程优先级的改变示例
public class SetPriority{
public static void main(String[] args){
MyThread threadA=new MyThread("threadA",8);
threadA.start();
MyThread threadB=new MyThread("threadB",2);
threadB.start();
MyThread threadC=new MyThread("threadC",7);
threadC.start();
try{
Thread.sleep(3000);
}catch(InterruptedException x){}
threadA.setPriority(3);
System.out.print("in main()-threadA.getPriority()="+threadA.getPriority());
}
}
class MyThread extends Thread{
public MyThread(String name,int i){
setPriority(i);
setName(name);
}
public void run(){
for(int i=0;i<5;i++){
System.out.print("priority="+getPriority()+",name="+getName());
try{
Thread.sleep(2000);
}catch(InterruptedException x){}
}
}
}
线程调度
线程睡眠sleep()【是当前线程睡眠(停止执行)若干毫秒】
暂停线程yield()
链接线程join()【调用join()方法可是当前线程暂停执行,等待调用该方法的线程结束后再继续执行本线程。】
有三种调用格式:
public final void join() throws InterruptedException
public final void join(long millis) throws InterruptedException
public final void join(long millis,int nanos) throws InterruptedException
例:
演示线程对象的生命周期从创建到结束的过程,其间使用new()、start()、sleep()、interrupt()等方法改变线程的状态。
import java.awt.*;
import java.awt.event.*;
public class TestStatus extends WindowAdapter implements ActionListener{
Frame f;
static TestStatus.ThreadTest t1,t2;
public static void main(String[] args){
TestStatus w=new TestStatus();
w.display;
t1=w.new ThreadTest("Welcome to java world!");
t2=w.new ThreadTest("Welcome to study thread!");
t2.start();
t2.setButton(); //设置按钮状态
}
public void display(){
f=new Frame("Welcome");
f.setSize(200, 200);
f.setLocation(200, 140);
f.setBackground(Color.lightGray);
f.setLayout(new GridLayout(4,1));
f.addWindowListener(this);
f.setVisible(true);
}
public class ThreadTest extends Thread{
Panel p1;
Label lb1;
TextField tf1,tf2;
Button b1,b2;
int sleeptime=(int)(Math.random()*100);
public ThreadTest(String str){
super(str);
for(int i=0;i<100;i++)
str=str+"";
tf1=new TextField(str);
f.add(tf1);
p1=new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
lb1=new Label("sleep");
tf2=new TextField(""+sleeptime);
p1.add(b1);
p1.add(b2);
b1.addActionListener(new TestStatus());
b2.addActionListener(new TestStatus());
f.add(p1);
f.setVisible(true);
}
public void run(){
String str;
while(this.isAlive()&&!this.isInterrupted()){ //线程活动且没中断时
try{
str=tf1.getText();
str=str.substring(1)+str.substring(0, 1);
tf1.setText(str);
this.sleep(sleeptime);
}catch(InterruptedException e){ //中断时抛出
System.out.print(e);
break; //退出循环
}
}
}
public void setButton()
if(this.isAlive()) b1.setEnabled(false);
if(this.isInterrupted()) b2.setEnabled(false);
}
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void actionPerformed(ActionEvent e){ //单击按钮时触发
if((e.getSource()==t1.b1)||(e.getSource()==t1.b2))
actionPerformed(e,t1);
if((e.getSource()==t2.b1)||(e.getSource()==t2.b2))
actionPerformed(e,t2);
}
public void actionPerformed(ActionEvent e,ThreadTest t1){ //重载
if(e.getSource()==t1.b1){ //启动
t1.sleeptime=Integer.parseInt(t1.tf2.getText());
t1.start();
}
if(e.getSource()==t1.b2)
t1.interrupt();
t1.setButton(); //设置按钮状态
}
}
多线程问题及处理
临界资源:在多线程编程中,被多个线程同时访问的资源就叫做临界资源。
线程的执行顺序是随机的,不受控制的。
解决临界资源问题的最基本、最简单的思路是使用同步关键字synchronized。synchronized关键字是一个修饰符,可以修饰方法或代码块。
synchronized的作用:对同一个对象(不是一个类的不同对象),当多个线程都同时调用该方法或代码块时,必须依次执行。
例:
class Tickets{
public int tickets;
public Tickets(){
tickets=10;
}
public synchronized void action(String name){
System.out.print(name+"抢到了第"+tickets+"号票");
tickets--;
}
}
//访问数据的线程
class TicketsThread extends Thread{
Tickets t;
String name;
public TicketsThread(Tickets t,String name){
this.t=t;
this.name=name;
start();
}
public void run(){
try{
for(int i=0;i<5;i++){
t.action(name);
Thread.sleep(20);
}
}catch(Exception e){}
}
}
//测试多线程的访问时问题
public class TestMulThread2{
public static void main(String[] agrs){
Tickets t=new Tickets();
TicketsThread d1=new TicketsThread(t,"小王");
TicketsThread d2=new TicketsThread(t,"小张");
}
}