2. notify():唤醒线程池中的一个线程(任何一个都有可能)。
3. notifyAll():唤醒线程池中的所有线程。
class Res
{
String name;
String sex;
boolean flag = false;
}
class Input implements Runnable//输入
{
private Res r;
//Object obj = new Object();
Input(Res r)
{
this.r = r;
}
public void run()
{
int x=0;
while(true)
{
synchronized(r)
{
if(r.flag)
try{r.wait();}catch(Exception e){}
if(x==0)
{
r.name= "mike";
r.sex="man";
}
else
{
r.name = "丽丽";
r.sex = "女女女女女";
}
x=(x+1)%2;
r.flag = true;
r.notify();
}
}
}
}
class Output implements Runnable//输出
{
//Object obj = new Object();
private Res r;
Output(Res r)
{
this.r = r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if(!r.flag)
try{r.wait();}catch(Exception e){}
System.out.println(r.name+"....."+r.sex);
r.flag = false;
r.notify();
}
}
}
}
class InputOutputDemo
{
public static void main(String[] args)
{<span style="white-space:pre"> </span>
Res r = new Res();//创建资源
Input in = new Input(r);//创建任务
Output out = new Output(r);
Thread t1 = new Thread(in);//创建线程,执行路径
Thread t2 = new Thread(out);
t1.start();//开启线程
t2.start();
}
}
二、JDK1.5新特性
2.lock():获取锁。
3.unlock():释放锁,为了防止异常出现,导致锁无法被关闭,所以锁的关闭动作要放在finally中。
2.Condition接口中的await方法对应于Object中的wait方法。
3.Condition接口中的signal方法对应于Object中的notify方法。
4.Condition接口中的signalAll方法对应于Object中的notifyAll方法。
import java.util.concurrent.locks.*;
class ProducerConsumerDemo2
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
// t1 t2
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name)throws InterruptedException
{
lock.lock();
try
{
while(flag)
condition_pro.await();//t1,t2
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
condition_con.signal();
}
finally
{
lock.unlock();//释放锁的动作一定要执行。
}
}
// t3 t4
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
flag = false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.set("+商品+");
}
catch (InterruptedException e)
{
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.out();
}
catch (InterruptedException e)
{
}
}
}
}
停止线程
class StopThread implements Runnable{
private boolean flag = true;
public void run(){
while(flag ){
System. out.println(Thread.currentThread().getName() +"...");
}
}
public void setFlag(){
flag = false ;
}
}
class StopThreadDemo{
public static void main(String[] args){
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.start();
t2.start();
int num = 1;
for(;;){
if(++num == 50){
st.setFlag();
break;
}
System. out.println("main..." + num);
}
System. out.println("over" );
}
}
1.将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java虚拟机退出。该方法必须在启动线程前调用。
2.该方法首先调用该线程的checkAccess方法,且不带任何参数。这可能抛出SecurityException(在当线程中)。
join方法:
public final void join()
throws InterruptedException
等待该线程终止。
抛出:
InterruptedException — 如果任何线程中断了当前线程。当抛出该异常时,当前线程的中断状态被清除。
class Demo implements Runnable
{
public void run()
{
for(int x=0; x<70; x++)
{
System.out.println(Thread.currentThread().toString()+"....."+x);
Thread.yield();
}
}
}
class JoinDemo
{
public static void main(String[] args) throws Exception
{
Demo d = new Demo();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
t1.start();
//t1.setPriority(Thread.MAX_PRIORITY);
t2.start();
//t1.join();
for(int x=0; x<80; x++)
{
//System.out.println("main....."+x);
}
System.out.println("over");
}
}
setPriority方法:
public final void setPriority(int newPriority)
更改线程的优先级。
首先调用线程的checkAccess方法,且不带任何参数。这可能抛出SecurityException。
在其他情况下,线程优先级被设定为指定的newPriority和线程的线程组的最大允许优先级相比较小的一个。
参数:newPriority—要为线程设定的优先级
toString方法:
public String toString()
返回该线程的字符串表示形式,包括线程名称、优先级和线程组。
覆盖:类object中的toStirng
返回:该线程的字符串表示形式。
yield方法:
public static void yield()
暂停当前正在执行的线程对象,并执行其他线程。
线程的优先级
线程优先级共10级(1~10),所有线程默认为5;
最高优先级:MAX_PRIORITY,
最低优先级:MIN_PRIORITY,
默认优先级:NORM_PRIORITY;
例:设置/修改某一个线程的优先级:t1.setPriority(Thread.MAX_PRIORITY);