码迷,mamicode.com
首页 > 编程语言 > 详细

java学习---线程

时间:2014-06-14 10:25:58      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   java   tar   ext   

1、继承Thread类,实现run方法

class TestThread
{
	public static void main(String[] args)
	{
		Thread1 t1=new Thread1();
		t1.start();
		int index=0;
		while(true)
		{
			if(index++==500)
			{
				t1.stopThread();
				t1.interrupt();
				break;
			}
			System.out.println(Thread.currentThread().getName());
		}
		System.out.println("main() exit");
	}
}

class Thread1 extends Thread
{
	private boolean bStop=false;
	public synchronized void run()
	{
		while(!bStop)
		{
			try
			{
				wait();
			}
			catch(InterruptedException e)
			{
				//e.printStackTrace();
				if(bStop)
					return;
			}
			System.out.println(getName());
		}
	}
	public void stopThread()
	{
		bStop=true;
	}
}

=========================================================================

2、实现Runable接口。

class TicketsSystem
{
	public static void main(String[] args)
	{
		SellThread st=new SellThread();
		
		new Thread(st).start();
		try
		{
			Thread.sleep(1);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		st.b=true;
		new Thread(st).start();
		//new Thread(st).start();
		//new Thread(st).start();
	}
}

class SellThread implements Runnable
{
	int tickets=100;
	Object obj=new Object();
	boolean b=false;
	public void run()
	{
		if(b==false)
		{
			while(true)
				sell();
		}
		else
		{
			while(true)
			{
				synchronized(obj)
				{
					try
					{
						Thread.sleep(10);
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
					synchronized(this)
					{
						if(tickets>0)
						{
							
							System.out.println("obj:"+Thread.currentThread().getName()+
									" sell tickets:"+tickets);
							tickets--;
						}
					}
				}
			}
		}
	}
	public synchronized void sell()
	{
		synchronized(obj)
		{
			if(tickets>0)
			{
				try
				{
					Thread.sleep(10);
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				System.out.println("sell():"+Thread.currentThread().getName()+
						" sell tickets:"+tickets);
				tickets--;
			}
		}
	}
}

==============================================================================================

3、利用内部类实现线程

class MultiThread
{
	public static void main(String[] args)
	{
		MyThread mt=new MyThread();
		/*new Thread(mt).start();
		new Thread(mt).start();
		new Thread(mt).start();
		new Thread(mt).start();*/
		mt.getThread().start();
		mt.getThread().start();
		mt.getThread().start();
		mt.getThread().start();
		//mt.setDaemon(true);
		//mt.setPriority(Thread.MAX_PRIORITY);
		//mt.start();
		int index=0;
		while(true)
		{
			/*if(index++==1000)
				break;*/
			System.out.println("main:"+Thread.currentThread().getName());
		}
	}
}

class MyThread //implements Runnable//extends Thread
{
	int index=0;
	private class InnerThread extends Thread
	{
		public void run()
		{
			while(true)
			{
				System.out.println(Thread.currentThread().getName()+":"+index++);
			}
		}
	}
	Thread getThread()
	{
		return new InnerThread();
	}
	/*public void run()
	{
		while(true)
		{
			System.out.println(Thread.currentThread().getName()+":"+index++);
			//yield();
		}
	}*/
}

===========================================================================

4、同步代码块synchronized

package cn.sunzn.synchronize;

public class SynchronizeCode {
   public static void main(String[] args) {
       new Thread() {
           public void run() {
               while (true) {
                   System.out.println("同步代码");
               }
           };
       }.start();
       new Thread() {
           public void run() {
               while (true) {
                   System.out.println("SynchronizeCode");
               }
           };
       }.start();
   }
}

=================================================================================

5、线程中的wait、notify-实现成产者和消费者设计模式

class Test
{
	public static void main(String[] args)
	{
		Queue q=new Queue();
		Producer p=new Producer(q);
		Consumer c=new Consumer(q);
		p.start();
		c.start();
	}
}

class Producer extends Thread
{
	Queue q;
	Producer(Queue q)
	{
		this.q=q;
	}
	public void run()
	{
		for(int i=0;i<10;i++)
		{
			q.put(i);
			System.out.println("Producer put "+i);
		}
	}
}
class Consumer extends Thread
{
	Queue q;
	Consumer(Queue q)
	{
		this.q=q;
	}
	public void run()
	{
		while(true)
		{
			System.out.println("Consumer get "+q.get());
		}
	}
}
class Queue
{
	int value;
	boolean bFull=false;
	public synchronized void put(int i)
	{
		if(!bFull)
		{
			value=i;
			bFull=true;
			notify();
		}
		try
		{
			wait();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
			
	}
	public synchronized int get()
	{
		if(!bFull)
		{
			try
			{
				wait();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		bFull=false;
		notify();
		return value;
	}
}





java学习---线程,布布扣,bubuko.com

java学习---线程

标签:class   blog   code   java   tar   ext   

原文地址:http://blog.csdn.net/yyxhhx/article/details/30551165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!