惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
有赞技术团队
有赞技术团队
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
雷峰网
雷峰网
T
Tor Project blog
博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
J
Java Code Geeks
AWS News Blog
AWS News Blog
Security Latest
Security Latest
Spread Privacy
Spread Privacy
T
Threatpost
博客园 - 三生石上(FineUI控件)
I
Intezer
G
Google Developers Blog
Scott Helme
Scott Helme
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
A
Arctic Wolf
F
Full Disclosure
P
Proofpoint News Feed
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threat Research - Cisco Blogs
B
Blog
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
Know Your Adversary
Know Your Adversary

博客园 - 泽来

设计原则 适配器模式 java复习进程 java小点滴 转>java5.0新特性 面试题收集 我买的书 java编程思想中关于多态性的描述 javascipt正则表达式 - 泽来 - 博客园 javascript小代码 - 泽来 - 博客园 DataGrid数据在Execel中打开 - 泽来 - 博客园 javascript高级教程 智力题收集 asp.net的TextBox回车触发事件 - 泽来 - 博客园 Builder生成器模式 单件模式(singleton) 是男人就坚持100下(强力益智,请留下战绩) XMLHTTP简介 - 泽来 - 博客园 多级无刷新联动(GB2312编码转换为UTF-8未取得满意效果) - 泽来 - 博客园
java多线程---线程之间的通信
泽来 · 2006-03-05 · via 博客园 - 泽来

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){};

//信息类
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;    
    }

}