在另一个线程中使用线程类的对象。 [英] Using an object of thread class in another thread.

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

问题描述

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.

推荐答案

看起来您的SalesPerson线程已经结束。在运行功能中需要无限循环。考虑一些等待,或覆盖一些标志的暂停和恢复功能来控制运行循环。



采取清晰而仔细的看 at错误消息您的控制台输出以确定发生了什么。
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆