java多线程---线程之间的通信
1.setDaemon(boolean); 为true 创建后面线程,在调用线程的start()方法之前调用。如果一个进程中没有任何前台线程,就算有后台线程在运行,整个进程仍然会结束。
isDaemon()判断是不是后面进程
Thread tt = new Thread();
tt.isDaemon();
2.setPriority(); 目前windows下只支持三个级别的优先级,分别是:
Thread.MAX_PRIORITY
Thread.MIN_PRIORITY
Thread.NORM_PRIORITY
Thread tt = new Thread();
tt.setPriority(Thread.MAX_PRIORITY);
3.join()
如果线程A执行时调用了线程B的join方法,那么线程A将会被挂起,待B执行完后再执行A,但join(millisecond)
有一个millisecond参数,如果过一规定时间线程B没有返回,将强制返回A. 应try{}catch(Exception e){};
isDaemon()判断是不是后面进程
Thread tt = new Thread();
tt.isDaemon();
2.setPriority(); 目前windows下只支持三个级别的优先级,分别是:
Thread.MAX_PRIORITY
Thread.MIN_PRIORITY
Thread.NORM_PRIORITY
Thread tt = new Thread();
tt.setPriority(Thread.MAX_PRIORITY);
3.join()
如果线程A执行时调用了线程B的join方法,那么线程A将会被挂起,待B执行完后再执行A,但join(millisecond)
有一个millisecond参数,如果过一规定时间线程B没有返回,将强制返回A. 应try{}catch(Exception e){};
//信息类
class Q
{
private String name = "unknown";
private String sex = "unknown";
boolean bFull = false;
public synchronized void put(String name, String sex)
{
if(bFull)
{
try{wait();}catch(Exception e){}
}
this.name = name;
//测试
try{Thread.sleep(1);}catch(Exception e){}
this.sex = sex;
bFull = true;
notify();
}
public synchronized void get()
{
if(!bFull)
{
try{wait();}catch(Exception e){}
}
System.out.print(name);
System.out.println(":"+sex);
bFull = false;
notify();
}
}
//生产者类
class Producer implements Runnable
{
Q q;
public Producer(Q q)
{
this.q = q;
}
public void run()
{
int i = 0;
while(true)
{
///////////////////////////////////////////////////////////////////////
/*synchronized(q)
{
if(q.bFull)
{
//如果缓冲区还有值,则此线程应该停止并将线程让给读取的线程
//必须用同步监时器的对象的wait和notify
try{q.wait();}catch(Exception e){}
}
if(i == 0)
{
q.name = "张三";
try{Thread.sleep(1);}catch(Exception e){}
q.sex = "男";
}
else
{
q.name = "李四";
q.sex = "女";
}
q.bFull = true;
q.notify();
}
*/
/////////////////////////////////////////
if( i == 0)
{
q.put("张三","男");
}
else
{
q.put("李四","女");
}
i = (i + 1) % 2;
}
}
}
//消费者类
class Consumer implements Runnable
{
Q q;
public Consumer(Q q)
{
this.q = q;
}
public void run()
{
q.get();
///////////////////////////////////////////////////////
/*synchronized(q)
{
while(true)
{
if(!q.bFull)
{
try{q.wait();}catch(Exception e){}
}
System.out.println("姓名是:"+q.name );
System.out.println("性别是:"+q.sex );
q.bFull = false;
q.notify(); //唤醒生产者线程
}
} */
//////////////////////////////////////////////////////////
}
}
//使用
public class lesson51
{
public static void main(String[] args)
{
//Q q = new Q();
//new Thread(new Producer(q)).start();
//new Thread(new Consumer(q)).start();
ThreadTest th = new ThreadTest();
new Thread(th).start();
for(int i = 0; i < 100; i++)
{
if(i == 50)
{
th.stopmyself();
}
System.out.println("main mothed is running " + i + "times");
}
}
}
class ThreadTest implements Runnable
{
private boolean bFlag = false;
public void run()
{
int i = 0;
while(!bFlag)
{
System.out.println("Thread name "+Thread.currentThread().getName()+ i++);
}
}
public void stopmyself()
{
bFlag = true;
}
}
class Q
{
private String name = "unknown";
private String sex = "unknown";
boolean bFull = false;
public synchronized void put(String name, String sex)
{
if(bFull)
{
try{wait();}catch(Exception e){}
}
this.name = name;
//测试
try{Thread.sleep(1);}catch(Exception e){}
this.sex = sex;
bFull = true;
notify();
}
public synchronized void get()
{
if(!bFull)
{
try{wait();}catch(Exception e){}
}
System.out.print(name);
System.out.println(":"+sex);
bFull = false;
notify();
}
}
//生产者类
class Producer implements Runnable
{
Q q;
public Producer(Q q)
{
this.q = q;
}
public void run()
{
int i = 0;
while(true)
{
///////////////////////////////////////////////////////////////////////
/*synchronized(q)
{
if(q.bFull)
{
//如果缓冲区还有值,则此线程应该停止并将线程让给读取的线程
//必须用同步监时器的对象的wait和notify
try{q.wait();}catch(Exception e){}
}
if(i == 0)
{
q.name = "张三";
try{Thread.sleep(1);}catch(Exception e){}
q.sex = "男";
}
else
{
q.name = "李四";
q.sex = "女";
}
q.bFull = true;
q.notify();
}
*/
/////////////////////////////////////////
if( i == 0)
{
q.put("张三","男");
}
else
{
q.put("李四","女");
}
i = (i + 1) % 2;
}
}
}
//消费者类
class Consumer implements Runnable
{
Q q;
public Consumer(Q q)
{
this.q = q;
}
public void run()
{
q.get();
///////////////////////////////////////////////////////
/*synchronized(q)
{
while(true)
{
if(!q.bFull)
{
try{q.wait();}catch(Exception e){}
}
System.out.println("姓名是:"+q.name );
System.out.println("性别是:"+q.sex );
q.bFull = false;
q.notify(); //唤醒生产者线程
}
} */
//////////////////////////////////////////////////////////
}
}
//使用
public class lesson51
{
public static void main(String[] args)
{
//Q q = new Q();
//new Thread(new Producer(q)).start();
//new Thread(new Consumer(q)).start();
ThreadTest th = new ThreadTest();
new Thread(th).start();
for(int i = 0; i < 100; i++)
{
if(i == 50)
{
th.stopmyself();
}
System.out.println("main mothed is running " + i + "times");
}
}
}
class ThreadTest implements Runnable
{
private boolean bFlag = false;
public void run()
{
int i = 0;
while(!bFlag)
{
System.out.println("Thread name "+Thread.currentThread().getName()+ i++);
}
}
public void stopmyself()
{
bFlag = true;
}
}