Topic: 线程与打印的问题 |
Print this page |
1.线程与打印的问题 | Copy to clipboard |
Posted by: ik9099 Posted on: 2004-08-06 14:47 我是一个JAVA的初学者,想请教各位前辈一个非常简单的问题,怎么能用3个线程分别打印出5,7,9的1-5的倍数.请多多指教 |
2.Re:请教各位前辈一个非常简单的问题 [Re: ik9099] | Copy to clipboard |
Posted by: bsspirit Posted on: 2004-08-07 11:17 程序写的有点长了,因为是新手,没做到代码复用,不过能实现功能。 public class ThreadTest1{ public static void main(String[] args){ Go1 g1 = new Go1(); Thread a1 = new Thread(g1); a1.start(); Go2 g2 = new Go2(); Thread a2 = new Thread(g2); a2.start(); Go3 g3 = new Go3(); Thread a3 = new Thread(g3); a3.start(); } } class Go1 implements Runnable{ public void run(){ for(int i=1;i<6;i++){ int k = 5 ; k *= i; System.out.println("5的" + i +"倍是 : "+ k); } } } class Go2 implements Runnable{ public void run(){ for(int i=1;i<6;i++){ int k = 7 ; k *= i; System.out.println("7的" + i +"倍是 : "+ k); } } } class Go3 implements Runnable{ public void run(){ for(int i=1;i<6;i++){ int k = 9 ; k *= i; System.out.println("9的" + i +"倍是 : "+ k); } } } |
3.Re:请教各位前辈一个非常简单的问题 [Re: ik9099] | Copy to clipboard |
Posted by: zslzx Posted on: 2004-08-07 12:53 我也写一个 public class Test{ public static void main(String args[]){ MyThread t5 = new MyThread(5); MyThread t7 = new MyThread(7); MyThread t9 = new MyThread(9); t5.start(); t7.start(); t9.start(); } } class MyThread extends Thread{ private int input; public MyThread(int i){ input = i; } public void run(){ for(int i = 1; i < 6; i++){ System.out.println(input + "的" + i + "倍是:" + (i * input)); } } } |
4.Re:请教各位前辈一个非常简单的问题 (线程) [Re: ik9099] | Copy to clipboard |
Posted by: bsspirit Posted on: 2004-08-08 12:24 我的改进版的,和上面的老兄的差不多了! public class ThreadTest1_2{ public static void main(String[] args){ new Go(5).start(); new Go(7).start(); new Go(9).start(); } } class Go extends Thread{ private int a; Go(int i){ a = i; } public void run(){ int k = a; for(int i=1;i<6;i++){ a =k * i; System.out.println(k +" 的 " + i +" 倍是 "+ a); } } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |