LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2012, 03:46 PM   #1
varoluscuprens
LQ Newbie
 
Registered: Jul 2011
Location: Turkey
Distribution: ubuntu 14.04
Posts: 17

Rep: Reputation: Disabled
Thread Execution Line Question


Hi There;
I have a Java code.Three threads have deadlocked themselves.I expect the output in following sequence:
Thread1,Thread2,Thread3
but in case of execution, the following sequence is observed:
Thread1,Thread3,Thread2

Why? Thanks in advance.

Code:
package p95uyg_2;

public class TripleDeadLock {

	public static void main(String[] args)
	{
		final Object resource1 = "resource1";
		final Object resource2 = "resource2";
		final Object resource3 = "resource3";
		
		Thread t1 = new Thread()
		{
			public void run()
			{
				synchronized(resource1)
				{
					System.out.println("Thread1: locked resource1");
					
					try
					{
						Thread.sleep(50);
					}catch(InterruptedException inex)
					{}
					
					synchronized(resource2)
					{
						System.out.println("Thread1: locked resource2");
					}
				}
			}
		};
		
		Thread t2 = new Thread()
		{
			public void run()
			{
				synchronized(resource2)
				{
					System.out.println("Thread2: locked resource2");
					
					try
					{
						Thread.sleep(50);
					}catch(InterruptedException inex)
					{}
					
					synchronized(resource3)
					{
						System.out.println("Thread2: locked resource3");
					}
				}
			}
		};
		
		Thread t3 = new Thread()
	{
		public void run()
		{
			synchronized(resource3)
			{
				System.out.println("Thread3: locked resource3");
				
				try
				{
					Thread.sleep(50);
				}catch(InterruptedException inex)
				{}
				
				synchronized(resource1)
				{
					System.out.println("Thread1: locked resource3");
				}
			}
		}
	};
	
		t1.start();
		t2.start();
		t3.start();
		
	}
}
 
Old 05-03-2012, 08:50 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
When a thread becomes runnable, it nevertheless remains uncertain which one will run next (and on which CPU(s) or core(s).

In addition, it is uncertain in what order their terminal-output will appear.

Generating "terminal output" at all changes the behavior, since the output generation is serialized.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
delay in thread execution..!! manohar Programming 5 11-26-2011 02:15 PM
Multi-thread execution efficiency problem cdcshu Linux - Software 3 08-02-2007 12:46 AM
pthreads.c - summary of thread execution times h/w Programming 1 06-15-2007 03:23 PM
Kernel Thread Execution Context and PID ratwings Linux - Newbie 0 02-11-2005 04:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration