本文介绍了在另一个线程中使用线程类的对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quote:



1.创建SalesPersons类作为将显示五个销售人员姓名的线程。

2.创建一个类作为Days作为其他具有七天数组的线程。

3.在几天内调用SalesPersons实例并启动它们

线程

4.周日暂停SalesPersons并在周三恢复

注意:使用挂起,从线程恢复方法


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();
	}

}





但是,调用suspend()和恢复时出错Days类中的方法。



But, there is an error while invoking suspend() and resume() methods within the Days class.

推荐答案


这篇关于在另一个线程中使用线程类的对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:16