class Do8
{
public static void main(String[] args)
{
Resource r =new Resource(); Input in =new Input(r);
Output out=new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
} class Resource
{
String name;
String sex;
boolean flag=false;
}
//输入
class Input implements Runnable
{
Resource r;
Input(Resource 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="往里";
r.sex="男";
}
else
{
r.name="xiaoli";
r.sex="wumen";
}
r.flag=true;
r.notify();//启动任意的停止的线层 } x=(x+1)%2; }
}
}
//输出
class Output implements Runnable
{
Resource r;
Output(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if(!r.flag)
{
try{r.wait();}catch(Exception e){}//不为真的时候,当前线层停止
}
try{Thread.sleep(100);}catch(Exception e){}
System.out.println(r.name+"..."+r.sex);
r.flag=false;
r.notify();//启动任意的停止的线层 }
}
}
}

练习

class Do9
{
public static void main(String[] args)
{
Resource r=new Resource();
Shengchan sc=new Shengchan(r);
Xiaoshou xs=new Xiaoshou(r);
Thread th1=new Thread(sc);
Thread th2=new Thread(xs);
th1.start();
th2.start();
}
} class Resource
{
private String name;
private int count=1;
private boolean flag=false;
public synchronized void set(String name)
{
if(flag)
try{this.wait();}catch(InterruptedException e){}
this.name=name+count;
count++;
System.out.println(Thread.currentThread().getName()+"..生产者.."+this.name);
flag=true;
notify();
}
public synchronized void out()
{
if(!flag)
try{this.wait();}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"..消费者........"+this.name);
flag=false;
notify();
}
}
class Shengchan implements Runnable
{
private Resource r; Shengchan(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
try{Thread.sleep(150);}catch(InterruptedException e){}
r.set("烤鸭");
}
} }
class Xiaoshou implements Runnable
{
private Resource r;
Xiaoshou(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
try{Thread.sleep(150);}catch(InterruptedException e){}
r.out();
}
}
}

java线层的启动与停止的更多相关文章

  1. java -jar shell 启动、停止

    启用 vi start.sh #!/bin/sh # ################################################################## # Powe ...

  2. Java线程的启动和停止(一)

    如何构造线程 在运行线程之前需要先构造线程对象,线程对象的构造需要指定线程所需要的属性,比如:所属线程组.线程优先级.是否为Daemon线程等信息.下面我们看一下,java.lang.Thread中对 ...

  3. Linux下启动和停止Java应用程序的Shell脚本

    转自:http://blog.csdn.net/jadyer/article/details/7960802 资料参考来源自兔大侠,并略作修改:http://www.tudaxia.com/archi ...

  4. shell脚本批量/单独启动、停止、重启java独立jar程序

    本人最近半年使用阿里dubbo做开发,并在公司内部大力进行推广,将原来一个笨重且不易于维护的大项目切分成多个相对独立的java程序,好处是显而易见的,但是随着切分的独立运行程序包越来越多,程序的部署变 ...

  5. (转)解决:本地计算机 上的 OracleOraDb10g_home1TNSListener服务启动后停止

    原文地址:http://justsee.iteye.com/blog/1320059 手动启动一个问题:本地计算机 上的 OracleOraDb10g_home1TNSListener服务启动后停止. ...

  6. CentOS7 增加tomcat 启动,停止,使用systemctl进行配置

    1,centos7 使用 systemctl 替换了 service命令 参考:redhat文档: https://access.redhat.com/documentation/en-US/Red_ ...

  7. Jenkins启动、停止脚本

    1.jenkins下载地址:http://pan.baidu.com/s/1o79ZRzs 2.创建shell脚本,如:jenkins.sh #!/bin/bash pid=`ps -ef | gre ...

  8. JBoss7 如何用脚本 启动 和 停止

    用脚本来启动/停止JBoss服务器,有助于开发部署的 自动执行,提高工作效率. 在JBoss以前的版本中,很容易在bin目录下面找到 启动和停止服务器的脚本: run.bat shutdown.bat ...

  9. nginx 的安装、启动、停止与重启

    一.nginx 基本介绍 1.Nginx 是单进程单线程模型,也就是启动的工作进程只有一个线程响应客户端请求,而 apache 可以在一个进程内启动多个线程响应客户端请求.所以 nginx 的内存占用 ...

随机推荐

  1. Hibernate数据库对象的创建与导出

    Hibernate 与数据库的关系是ORM关系,对象映射数据库. 那么如何通过对象对数据库进行各种对象的ddl与dml操作呢? 数据库对象操作的〈database-object /〉+ SchemaE ...

  2. php发送post请求的三种方法示例

    本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...

  3. awk的思维导图

  4. NMAP扫描UDP123NTP端口详解

    我用的nmap版本:Zenmap 6.25 例如命令: nmap -sU -pU:123 -Pn -n --script=ntp-monlist IP 如果要批量进行,可以把IP存放在ip.txt可以 ...

  5. HP Webinspect 10 访问wap的url

    HP Webinspect是著名的扫描工具,这里讲一下怎么使用它扫wap的url. 通俗的讲,Wap是手机网页浏览器使用的网页,web是电脑网页浏览器使用的网页.(讲得不专业,但方便理解) 在手机上显 ...

  6. SSD、高级格式化硬盘,4K,分区,对齐,Ghost能不能用的解释用SSD的都可以看看

    或者以上,建议选择2048,这是Win7分区使用的值,实际对齐大小就为1024K.  4.如何知道是否对齐?  使用工具AS SSD Benchmark(HDTune Pro 4.6目前测的禁绝)   ...

  7. 转:SetWindowText 的用法

    SetWindowText   函数功能:该函数改变指定窗口的标题栏的文本内容(如果窗口有标题栏).如果指定窗口是一个控件,则改变控件的文本内容.然而,SetWindowText函数不改变其他应用程序 ...

  8. MSSQL2005 导出excel文件

    Title:MSSQL2005 导出excel文件  --2011-01-16 16:01 EXEC master..xp_cmdshell 'bcp "select * from 数据库名 ...

  9. 我学hash_map(1)

    本文来源:http://blog.chinaunix.net/uid-26548237-id-3800125.html  map是什么? map是键值对(key-value),复杂度是O(n).但是查 ...

  10. HDU-3665(单源最短路)

    Problem Description XiaoY is living in a big city, there are N towns in it and some towns near the s ...