ThreadDemo.java
public class ThreadDemo
{
public static void main(String []args)
{
//Thread tt = new TestThread();
//tt.setDaemon(true);
/*Thread tt = new Thread(new TestThread());
tt.start();//.run();
int index=0;
while(true)
{
if(index++ == 100)
try{tt.join(10000);}catch(Exception e){}
System.out.println("main():"+Thread.currentThread().getName());
}*/
/*new TestThread().start();
new TestThread().start();
new TestThread().start();
new TestThread().start();*/
TestThread tt = new TestThread();
/*tt.start();
tt.start();
tt.start();
tt.start();*/
new Thread(tt).start(); //并不是马上执行,只是就绪状态;继续main线程的执行。
try{Thread.sleep(1);}catch(Exception e){}
tt.str = new String("method");
new Thread(tt).start();
//new Thread(tt).start();
//new Thread(tt).start();
}
}
class TestThread implements Runnable //extends Thread
{
int tickets = 100;
String str = new String("");
public void run()
{
if(str.equals("method"))
{
while(true)
{
sale();
}
}
else
{
while(true)
{
//System.out.println("run():"+Thread.currentThread().getName());
synchronized(str) //同步代码块 str是同步对象
{
if(tickets > 0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.print("同步代码块():");
synchronized(this){}
System.out.println(Thread.currentThread().getName()+" is saling ticket "+tickets--);
}
}
}
}
}
public synchronized void sale() //同步函数
{
if(tickets > 0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.print("sale函数():");
synchronized(str){}
System.out.println(Thread.currentThread().getName()+" is saling ticket "+tickets--);
}
}
}
生产者与消费者问题
停止线程的方式
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)
try{q.wait();}catch(Exception e){}
if(i == 0)
{
q.name = "zhangsan";
try{Thread.sleep(1);}catch(Exception e){}
q.sex = "male";
}
else
{
q.name = "lisi";
q.sex = "female";
}
q.bFull=true;
q.notify();
}*/
if(i == 0)
{
q.put("zhangsan", "male");
}
else
{
q.put("lisi", "female");
}
i = (i+1)%2;
}
}
}
class Consumer implements Runnable
{
Q q;
public Consumer(Q q)
{
this.q = q;
}
public void run()
{
while(true)
{
/*synchronized(q)//解决线程同步问题--代码块
{
if(!q.bFull)
try{q.wait();}catch(Exception e){}//解决线程通信问题--等待
System.out.print(q.name);
System.out.println(":"+q.sex);
q.bFull=false;
q.notify();//解决线程通信问题--呼唤
}*/
q.get();
}
}
}
class Q//这样的类叫做线程安全的类,可以用于多线程操作。
{
private String name="unknown";
private String sex="unknown";
private boolean bFull = false;
public synchronized void put(String name,String sex)//解决线程同步问题--同步函数,对象为锁旗标
{
if(this.bFull)
try{this.wait();}catch(Exception e){}//解决线程通信问题--等待
this.name = name;
try{Thread.sleep(1);}catch(Exception e){}
this.sex = sex;
this.bFull = true;
this.notify();//解决线程通信问题--呼唤
}
public synchronized void get()
{
if(!this.bFull)
try{this.wait();}catch(Exception e){}//解决线程通信问题--等待
System.out.print(this.name);
System.out.println(":"+this.sex);
this.bFull = false;
this.notify();//解决线程通信问题--呼唤
}
}
class ThreadCommunation
{
public static void main(String []args)
{
/*Q q = new Q();
new Thread(new Producer(q)).start();
new Thread(new Consumer(q)).start();*/
ThreadTest tt = new ThreadTest();
new Thread(tt).start();
for(int i=0;i<100;i++)
{
if(i==50)
{
tt.stopMe();
}
System.out.println("main() is running.");
}
}
}
class ThreadTest implements Runnable
{
boolean bFlag = false;
public void stopMe()
{
bFlag = true;
}
public void run()
{
while(!bFlag)
{
System.out.println(Thread.currentThread().getName()+"is running.");
}
}
}