谁帮我实现以下Java中的线程交替执行控制,要求按顺序输出1到100,代码如下。
packagecom.leejiliang.HomeworkDemo2;/***定义打印输出类,定义两个数据输出方法,分别用于输出偶数和奇数*@authorAdminis...
package com.leejiliang.HomeworkDemo2;
/**
* 定义打印输出类,定义两个数据输出方法,分别用于输出偶数和奇数
* @author Administrator
* even偶数 uneven奇数
*/
class PrintNumber{
public void printEven(){
for(int i=0;i<=100;i++){
if(i%2==0){
System.out.println("偶数:"+i);
}
}
}
//输出1到100之间的奇数
public void printUnEven(){
for(int i=1;i<=100;i++){
if(i%2!=0){
System.out.println("奇数:"+i);
}
}
}
}
/**
*
* @author Administrator
* 定义输出偶数的线程类
*/
class ThreadForEven implements Runnable{
private PrintNumber pn=null;
public ThreadForEven(PrintNumber pn) {
this.pn=pn;
}
@Override
public void run() {
pn.printEven();
}
}
/**
* 定义输出奇数的线程类
* @author Administrator
*
*/
class ThreadForUneven implements Runnable{
private PrintNumber pn=null;
public ThreadForUneven(PrintNumber pn) {
this.pn=pn;
}
@Override
public void run() {
pn.printUnEven();
}
}
//主函数,执行两个线程
class ZeroToOneHundred{
public static void main(String[] args) {
PrintNumber pn=new PrintNumber();
ThreadForEven te=new ThreadForEven(pn);
ThreadForUneven tu=new ThreadForUneven(pn);
Thread threadeven=new Thread(te);
Thread threaduneven =new Thread(tu);
threadeven.start();
threaduneven.start();
}
} 展开
/**
* 定义打印输出类,定义两个数据输出方法,分别用于输出偶数和奇数
* @author Administrator
* even偶数 uneven奇数
*/
class PrintNumber{
public void printEven(){
for(int i=0;i<=100;i++){
if(i%2==0){
System.out.println("偶数:"+i);
}
}
}
//输出1到100之间的奇数
public void printUnEven(){
for(int i=1;i<=100;i++){
if(i%2!=0){
System.out.println("奇数:"+i);
}
}
}
}
/**
*
* @author Administrator
* 定义输出偶数的线程类
*/
class ThreadForEven implements Runnable{
private PrintNumber pn=null;
public ThreadForEven(PrintNumber pn) {
this.pn=pn;
}
@Override
public void run() {
pn.printEven();
}
}
/**
* 定义输出奇数的线程类
* @author Administrator
*
*/
class ThreadForUneven implements Runnable{
private PrintNumber pn=null;
public ThreadForUneven(PrintNumber pn) {
this.pn=pn;
}
@Override
public void run() {
pn.printUnEven();
}
}
//主函数,执行两个线程
class ZeroToOneHundred{
public static void main(String[] args) {
PrintNumber pn=new PrintNumber();
ThreadForEven te=new ThreadForEven(pn);
ThreadForUneven tu=new ThreadForUneven(pn);
Thread threadeven=new Thread(te);
Thread threaduneven =new Thread(tu);
threadeven.start();
threaduneven.start();
}
} 展开
2个回答
展开全部
稍微修改下 未测试
package com.leejiliang.HomeworkDemo2;
/**
* 定义打印输出类,定义两个数据输出方法,分别用于输出偶数和奇数
*
* @author Administrator even偶数 uneven奇数
*/
class PrintNumber {
boolean isEven = true;
public void printEven() {
for (int i = 0; i <= 100; i = i + 2) {
if (i % 2 == 0) {
synchronized (this) {
while (!isEven) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("偶数:" + i);
isEven = false;
this.notify();
}
}
}
}
// 输出1到100之间的奇数
public void printUnEven() {
for (int i = 1; i <= 99; i = i + 1) {
if (i % 2 != 0) {
synchronized (this) {
while (isEven) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("奇数:" + i);
isEven = true;
this.notify();
}
}
}
}
}
/**
*
* @author Administrator 定义输出偶数的线程类
*/
class ThreadForEven implements Runnable {
private PrintNumber pn = null;
public ThreadForEven(PrintNumber pn) {
this.pn = pn;
}
@Override
public void run() {
pn.printEven();
}
}
/**
* 定义输出奇数的线程类
*
* @author Administrator
*
*/
class ThreadForUneven implements Runnable {
private PrintNumber pn = null;
public ThreadForUneven(PrintNumber pn) {
this.pn = pn;
}
@Override
public void run() {
pn.printUnEven();
}
}
// 主函数,执行两个线程
class ZeroToOneHundred {
public static void main(String[] args) {
PrintNumber pn = new PrintNumber();
ThreadForEven te = new ThreadForEven(pn);
ThreadForUneven tu = new ThreadForUneven(pn);
Thread threadeven = new Thread(te);
Thread threaduneven = new Thread(tu);
threadeven.start();
threaduneven.start();
}
}
2015-06-23
展开全部
感觉是要两个线程。。。。。。。。但楼主这那么多线程,看着迷糊了
追问
就两个线程啊,可能我描述的不清楚!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |