Click here to Skip to main content
15,937,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there are two threads one thread is for taking input to check odd or even number and the other is for taking string input how to avoid race condition..... thank you in advance

What I have tried:

Java
import java.util.Scanner;
class Palindrome implements Runnable
{
	Thread t;
	Palindrome()
	{
		t=new Thread(this,"Palindrome");
		t.start();
	}
	
	public void run()
	{
		PalindromeTest();
	}
	synchronized void PalindromeTest()
	{
		
			Scanner sc=new Scanner(System.in);
			System.out.println("Enter a string to check pallindrome : ");
			String s=sc.nextLine();
			String rev="";
			
			int size=s.length()-1;
			for(int i=0;i<=size;i++)
			{
				rev=rev + s.charAt(size-i);
			}
			if(rev.equalsIgnoreCase(s))
				System.out.println("The string is Pallindrome");
			else
				System.out.println("The string is not palindrome");
			notify();

	}
}

class OddEven implements Runnable
{
	Thread t;
	OddEven()
	{
		t=new Thread(this,"Palindrome");

		t.start();
	}
	
	public void run()
	{
		try
		{
			t.wait();
			OddEvenTest();
		}
		catch(InterruptedException e)
		{
			System.out.println(e);
		}
		
	}
	void OddEvenTest()
	{
			if(t.wait())
			{
				t.suspend();
			}
			Scanner sc=new Scanner(System.in);
			System.out.println("Enter a number to check odd or even : ");
			int s=sc.nextInt();
			if(s%2==0)
				System.out.println("The number "+s+ " is even");
			else
				System.out.println("The number "+s+ " is odd");

	}
}

class TestThread
{
	public static void main(String args[])
	{
		new Palindrome();
		new OddEven();
	}
}
Posted
Updated 9-Sep-16 4:07am
v2

1 solution

Quote:
there are two threads one thread is for taking input to check odd or even number and the other is for taking string input how to avoid race condition..... thank you in advance
Your statement is a race condition by definition because 2 threads are taking input and each input can be taken only once.

There is no solution to this.

All you can do is to explain in details what you want to do (without talking about threads).
 
Share this answer
 
Comments
Maciej Los 9-Sep-16 14:08pm    
Short And To The Point, 5!
Patrice T 9-Sep-16 15:25pm    
Thank you.
Member 12729842 11-Sep-16 0:55am    
ppolymorphe first of all the input for integer had to be taken but it is coming for string?? I want to solve this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900