现在的位置: 首页 > 综合 > 正文

线程互斥

2018年02月13日 ⁄ 综合 ⁄ 共 2594字 ⁄ 字号 评论关闭

 

package com.entel.research;

public class TraditionalThreadSynchronized
{
	public static void main(String[] args)
	{
		new TraditionalThreadSynchronized().init();
	}
	
	private void init()
	{
		final Outputer outputer = new Outputer();
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				while(true)
				{
					try
					{
						Thread.sleep(10);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					outputer.output("beijing");
				}
			}
		}).start();
		
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				while(true)
				{
					try
					{
						Thread.sleep(10);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					outputer.output("shanghai");
				}
			}
		}).start();
	}
	
	class Outputer
	{
		public void output(String name)
		{
			int len = name.length();
			synchronized (this)
			{
				for (int i = 0; i < len; i++)
				{
					System.out.print(name.charAt(i));
				}
			}
			System.out.println();
		}
	}
}

 

 

package com.entel.research;

public class TraditionalThreadSynchronized
{
	public static void main(String[] args)
	{
		new TraditionalThreadSynchronized().init();
	}

	private void init()
	{
		final Outputer outputer = new Outputer();
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				while (true)
				{
					try
					{
						Thread.sleep(10);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					outputer.output("beijing");
				}
			}
		}).start();

		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				while (true)
				{
					try
					{
						Thread.sleep(10);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					outputer.output2("shanghai");
				}
			}
		}).start();
	}

	static class Outputer
	{
		public void output(String name)
		{
			int len = name.length();
			synchronized (this)
			{
				for (int i = 0; i < len; i++)
				{
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
		}

		public synchronized void output2(String name)
		{
			int len = name.length();
			for (int i = 0; i < len; i++)
			{
				System.out.print(name.charAt(i));
			}
			System.out.println();
		}
	}
}

 

 

package com.entel.research;

public class TraditionalThreadSynchronized
{
	public static void main(String[] args)
	{
		new TraditionalThreadSynchronized().init();
	}

	private void init()
	{
		final Outputer outputer = new Outputer();
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				while (true)
				{
					try
					{
						Thread.sleep(100);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					outputer.output("beijing");
				}
			}
		}).start();

		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				while (true)
				{
					try
					{
						Thread.sleep(100);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					outputer.output3("shangsai");
				}
			}
		}).start();
	}

	static class Outputer
	{
		public void output(String name)
		{
			int len = name.length();
			synchronized (Outputer.class)
			{
				for (int i = 0; i < len; i++)
				{
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
		}

		public synchronized void output2(String name)
		{
			int len = name.length();
			for (int i = 0; i < len; i++)
			{
				System.out.print(name.charAt(i));
			}
			System.out.println();
		}
		
		public static synchronized void output3(String name)
		{
			int len = name.length();
			for (int i = 0; i < len; i++)
			{
				System.out.print(name.charAt(i));
			}
			System.out.println();
		}
	}
}

 

抱歉!评论已关闭.