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. 转载:解决linux ping: unknown host www.baidu.com

    解决linux ping: unknown host www.baidu.com 转载网址:http://www.kankanews.com/ICkengine/archives/48417.shtm ...

  2. Css3案例

    <!DOCTYPE html> <html> <meta charset=utf-> <head> <style> body{ backgo ...

  3. memcache的使用

    什么是memcache? Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等. ...

  4. office2003万能密钥

    保证有效 OFFICE 2003 :  GWH28-DGCMP-P6RC4-6J4MT-3HFDY

  5. IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)

    IOS学习:ios中的数据持久化初级(文件.xml.json.sqlite.CoreData) 分类: ios开发学习2013-05-30 10:03 2316人阅读 评论(2) 收藏 举报 iOSX ...

  6. C51的编程规范

    现在单片机的程序设计,C51已经得到广泛的推广和应用,算是单片机的主流设计程序,甚至可以说作为单片机开发人员必须要掌握的一门语言了.作为一门工具,最终的目的就是实现功能.在满足这个前提条件下,我们希望 ...

  7. Java 多线程之龟兔赛跑(文件夹——读取文件——时间)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 描述: 乌龟和兔子(各自是一个Java线程)在我们的电脑上赛跑,我们为它们指定一个跑道(本地文件系统上的一个目录,该目录包含子目录).跑的规则是读 ...

  8. P2P网贷投资须谨防圈钱人

    摘要:P2P领域依然呈现投资热潮,甚至部分国有大行也有意涉足.这个行业到底怎么了?P2P平台上高收益的理财产品,到底能买不?     新年才刚刚过了10天,就有4家网贷平台被爆出支付危机,P2P一时被 ...

  9. Apache HttpClient组件封装工具类

    package com.mengyao.spider.utils; import java.util.ArrayList;import java.util.HashMap;import java.ut ...

  10. 让iframe调用页面的背景透明

    需求:在一个div里面嵌套一个iframe(src="ui.html"),div有背景图片,要让iframe里的内容透明地显示在div层上. 刚开始直接把iframe放在div中, ...