Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Quote:

1. Create class of SalesPersons as a thread that will display fives sales persons name.
2. Create a class as Days as other Thread that has array of seven days.
3. Call the instance of SalesPersons in Days and start both the
threads
4. suspend SalesPersons on Sunday and resume on wednesday
Note: use suspend, resume methods from thread


What I have tried:

public class SalesPersons implements Runnable {

	Thread t;
	SalesPersons()
	{
		t=new Thread(this,"P.name");
		t.start();
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub

		System.out.println("P1, "+"P2, "+"P3, "+"P4, "+"P5");
	}

}


public class Days implements Runnable {

	Thread t1;
	SalesPersons obj;
	String day[]={"Sunday","Monday","Tueasday","Wednesday","Thursday","Friday","Saturday"}; 
	Days()
	{
		t1=new Thread(this,"day");
		obj=new SalesPersons();
		t1.start();
		
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<31;i++)
		{
			int j=i%7;
			if(day[j]=="Sunday")
			{
				obj.suspend();
			}
			else if(day[j]=="Wednesday")
			{
				obj.resume();
			}
		}
		
	}

}


public class MainClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		new Days();
		//new SalesPersons();
	}

}


But, there is an error while invoking suspend() and resume() methods within the Days class.
Posted
Updated 25-Jul-20 0:03am

Looks like your SalesPerson thread has ended. It need an endless loop in the run function. Consider some waiting, or overwrite the suspend and resume function for some flags to control the run loop.

Take a sharp and closer look at the error messages and your console output to figure out what is happening.
 
Share this answer
 
Comments
Member 13257407 6-Jul-17 14:27pm    
It is showing compilation error. The suspend() and resume() methods are underlined red when I type them in eclipse.
class SalesPerson implements Runnable {
	private String[] salesPersonName = { "sp1", "sp2", "sp3", "sp4", "sp5", "sp6", "sp7", "sp8" };
	private static boolean stopFlag = true;
	Thread th;

	public SalesPerson() {
		super();
		th = new Thread(this);
		th.start();
	}

	@Override
	public void run() {
		while (stopFlag) {
			try {
				int index = new Random().nextInt(7);
				System.out.println(salesPersonName[index]);
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean isStopFlag() {
		return stopFlag;
	}

	public void setStopFlag(boolean stopFlag) {
		SalesPerson.stopFlag = stopFlag;
	}

	public void suspend() {
		setStopFlag(false);
	}

	public void resume() {
		setStopFlag(true);
	}

}




class Days implements Runnable {
	private String[] days = { "Sunday", "Monday", "Tuesday", "Wednessday", "Thursday", "Friday", "Saturday" };
	private static boolean stopFlag = false;
	SalesPerson salesPerson;

	public Days(SalesPerson salesPerson) {
		super();
		this.salesPerson = salesPerson;
		Thread t = new Thread(this);
		t.start();
		salesPerson.suspend();
	}

	@Override
	public void run() {
		synchronized (this) {
			try {
				while (!stopFlag) {
					for (String day : days) {
						System.out.println(day);
						if (day.equals("Sunday") || day.equals("Monday") || day.equals("Tuesday")) {
							salesPerson.suspend();
						} else if (day.equals("Wednessday")) {
							salesPerson.resume();
							Thread.sleep(2500);
						} else {
							Thread.sleep(2500);
						}
					}
					salesPerson.suspend();
					stopFlag = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}


public class SalesPersonDaysThreadTask2 {
	public static void main(String[] args) {
		SalesPerson person = new SalesPerson();
		Thread t = new Thread(new Days(person));
		t.start();

	}
}
 
Share this answer
 
Comments
CHill60 27-Jul-20 5:38am    
An uncommented code dump is not a good solution. Point out the differences between your code and the OPs. Explain why yours works (assuming it does)

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