写一个多线程程序,其中一个线程在屏幕上输出从1到100这100个整数,另一个线程在屏幕上无限循环的输出它的线程名字。要求第一个线程在主线程结束之前结束,第二个线程在主线程结束时结束。
---------------------------------------------------------------
线程好久没用忘了,帮我写一个我看看吧,另外不太明白题目中的主线程。谢谢大家

解决方案 »

  1.   

    public class MulTheardTest{
      
       class threa1 extends Thread{
        public void run(){}//在屏幕上输出从1到100这100个整数,
       }
       class threa2 extends Thread{
        public void run(){}//在..
       }
       public void static main(){
          threa1  t1 = new threa1()
          threa2  t1 = new threa2()
          MulTheardTest mt = new MulTheardTest();
          t1.start();
          while(t1.isalive()){thread.sleep(time);}
          t2.start();     
       }
    }}
      

  2.   

    while(t1.isalive()){thread.sleep(time);} 
    time是什么
      

  3.   

    我写了一个, 虽然自己都不太满意, 但是效果基本达到了. 
    有四个类:
    SelfTalkThread用于打印自己的名字.
    CountThread答应1-100.
    Mutex用于做互斥标志, 因为我发现用String, 或者Integer都不可以. 因为这两个类都比较特殊. 用基本类型的数据的也不行, 因为基本类型的是值传递. 不能做多线程通信使用.
    Test是测试类, 也是主Thread.public class CountThread extends Thread{
    Mutex mutex;
    private int count = 0;
    CountThread(Mutex mutex){
    this.mutex = mutex;
    }
    public void run() {
    while(true){
    System.out.println(count++);
    try {
    sleep(50);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    if(count==100){
    synchronized(mutex){
    mutex.setValue(1);
    }
    System.out.println("CountThread: game over!");
    break;
    }
    }
    }

    }
    public class SelfTalkThread extends Thread{
    Mutex mutex;
    SelfTalkThread(Mutex mutex){
    this.mutex = mutex;
    }
    public void run() {
    while(true){
    System.out.println("SelfTalkThread ");
    try {
    sleep(40);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    synchronized(mutex){
    if(mutex.getValue()==1){
    System.out.println("SelfTalkThread: game over!");
    break;
    }
    }
    }
    }

    }
    public class Mutex {
    int value; public Mutex(){
    this.value = 0;
    }

    public int getValue() {
    return value;
    } public void setValue(int value) {
    this.value = value;
    }

    }public class Test { public static void main(String[] args) {
    Mutex mutex = new Mutex();
    CountThread cThread = new CountThread(mutex);
    SelfTalkThread sThread = new SelfTalkThread(mutex);
    cThread.start();
    sThread.start();
    }}不知道高手有没有好的做法.
      

  4.   

    怎么好像这个还比较困难吗?怎么要求线程2结束呢?destroy()吗?
      

  5.   

    class Runnable1 implements Runnable {
    public void run() {
    for (int i = 1; i <= 100; i++) {
    System.out.println(i);
    }
    }
    }class Runnable2 implements Runnable {
    public void run() {
    while (Test.b) {
    System.out.println(Thread.currentThread().getName());
    }
    }
    }public class Test {
    static boolean b = true;
    public static void main(String[] args) {
    Thread t1 = new Thread(new Runnable1(),"A");
    Thread t2 = new Thread(new Runnable2(),"B");
    t1.start();
    t2.start();
    try {
    t1.join();//等待线程一结束
    } catch (InterruptedException e) {
    e.printStackTrace();
    }finally{
    b = false;
    }
    }
    }
    ~!@#¥%……&*()
      

  6.   

    我用的是比较野蛮的方法 就是让主线程睡的时间久一点 这样就好了 没想到别的方法
    public class Csdm {//s 写一个多线程程序,其中一个线程在屏幕上输出从1到100这100个整数,另一个线程在屏幕上无限循环的输出它的线程名字。要求第一个线程在主线程结束之前结束,第二个线程在主线程结束时结束。
    public static void main(String[] args) {
    MyThread mt=new MyThread();
    MyThread1 mt1=new MyThread1();
    mt1.flag=true;

    new Thread(mt).start();
    new Thread(mt1).start();

    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    mt1.flag=false;
    System.out.println(Thread.currentThread().getName()+" 进程结束");
    }} class MyThread implements Runnable{
     
    public void run() {
    for(int i=1;i<=100;i++){
    System.out.print(i+"  ");
    }
    System.out.println(Thread.currentThread().getName()+" 进程结束");
    }
    }
     
     class MyThread1 implements Runnable{
      boolean flag=true;
     
    public void run() {
    System.out.println();
    while(flag){
    System.out.println(Thread.currentThread().getName());
    }
    System.out.println(Thread.currentThread().getName()+" 进程结束");
    }
    }