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. python之socket编程

    本章内容 1.socket 2.IO多路复用 3.socketserver Socket socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用[打开][读写][ ...

  2. 【转】Nginx windows下搭建过程

    Nginx windows下搭建过程 内容列表: 简要介绍 下载安装 配置测试 一.简要介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器, ...

  3. 避免Block的循环引用

    避免Block的循环引用 什么是循环引用,什么时候发生循环引用 1 循环引用就是当self 拥有一个block的时候,在block 又调用self的方法.形成你中有我,我中有你,谁都无法将谁释放的困局 ...

  4. 从win7到mac os再到win10,体验总结

    首先介绍自己的身份,软件工程毕业,现在IT男一枚,php程序员(前端必须会的啦). 原先自己用的是win7系统,大学四年都在用,所以习惯了.开发方式也是传统式的,以文件概念为主(就是手动管理文件),版 ...

  5. ui router digest 10 time

    refer : https://github.com/angular-ui/ui-router/issues/600 $urlRouterProvider.when("/", &q ...

  6. poj 3130 How I Mathematician Wonder What You Are!

    http://poj.org/problem?id=3130 #include <cstdio> #include <cstring> #include <algorit ...

  7. 关于百度地图InfoWindow响应自定义布局点击事件

    大概讲解: 在百度地图上显示一个marker,当marker被点击后,显示自定义的View.当自定义的View被点击后,响应不同Button的点击事件.被百度这个infowindo里面的view坑惨了 ...

  8. HDU_2018——母牛产小牛的问题,递推

    Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛?   Input 输入数据由多 ...

  9. vmware vms migration to openstack

    Converting a VMware Workstation virtual machine to KVM Leave a commentPosted by rbgeek on August 13, ...

  10. Spring MVC中各个filter的用法

    转载:http://blog.csdn.net/qyp1314/article/details/42023725 Spring MVC中各个filter的用法 2014-12-19 09:08 105 ...