⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadcom1.java

📁 用java编写的一些初级代码
💻 JAVA
字号:
class Q
{
    private String name="cat";
    private String sex="a";
    boolean bFull=false;
    synchronized void put(String name,String sex)
    {
	try{if(bFull)wait();}catch(Exception e){
	    System.out.println(e.getMessage());}

	this.name=name;
	try{Thread.sleep(10);}catch(Exception e){
	    System.out.println(e.getMessage());}
	this.sex=sex;
	bFull=true;
	notify();
    }
    synchronized void get(){
	try{if(!bFull)wait();}catch(Exception e){
	    System.out.println(e.getMessage());}
	System.out.println(name+"----->"+sex);
	bFull=false;
	notify();
    }
}

class Produce implements Runnable 
{
    Q q=null;
    public Produce(Q q){this.q=q;}
    public void run()
    {
	int i=0;
	while (true)
	{
	    if(i==0)q.put("dog","d");else q.put("cat","a");
	    i=(i+1)%2;
	}
    }
}
class Consume implements Runnable 
{
    Q q=null;
    public Consume(Q q){this.q=q;}
    public void run()
    {
	while (true)
	{
	    q.get();
	}
    }
}

public class ThreadCom1
{
    public static void main(String [] args)
    {
	Q q=new Q();
	new Thread(new Produce(q)).start();
	new Thread(new Consume(q)).start();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -