【问题标题】:Alternatively Printing using Multi-Threading或者使用多线程打印
【发布时间】:2013-11-05 15:31:21
【问题描述】:

我对程序运行的分析,但事实并非如此::

-我在 main 方法中创建了 2 个线程,即 Child1 和 Child2。

-然后两个线程都启动了

-Child1 作为单独的线程进入 run() 方法并进入同步块并打印 1 并由于调用了 wait 方法而休眠。

-Child2作为单独线程进入run()方法进入同步块并打印1并通知Child1唤醒。

-这个过程一直持续到5

package multi_threading;

public class inter_thread implements Runnable {
    static inter_thread obj;
    boolean val=false;
    Thread t;

    public inter_thread(){}
    public inter_thread(String msg){
        t=new Thread(obj,msg);
        t.start();
    }
    public static void main(String args[]){
        obj=new inter_thread();
        inter_thread obj1=new inter_thread("Child1"); 
        inter_thread obj2=new inter_thread("Child2");
        try{
            obj1.t.join();
            obj2.t.join();
        }catch(InterruptedException e){
            System.out.println("Interrupted");
        }
    }

    public void run(){
        int i;
        synchronized(obj){
        for(i=1;i<=5;i++){
            System.out.println(i);
            val=!val;
            while(val)
                try{
                    wait();
                }catch(InterruptedException e){
                    System.out.println("Interrupted");
            }
            notify();
        }
     }
    }

}

我想使用多线程显示这样的输出::

1
1
2
2
3
3
4
4
5
5

输出::

1
1
2

谁能告诉我是什么问题?

EDIT2::我已经编辑了之前的代码

【问题讨论】:

  • 你还没有初始化Thread t;。此外,您正在this 上进行同步,并且您正在使用两个不同的实例。您应该在共享对象上同步并在该对象上调用notifywait
  • 此外,由于两个线程都在不同的 inter_thread 实例上工作,它们不会修改相同的 val 字段。因此,在第一个循环之后,两个线程将永远等待,因为“他们的”val 字段始终为真。

标签: java multithreading nullpointerexception runtimeexception


【解决方案1】:

Thread t; 未初始化。

System.out.println(t.getName()+" has "+i); // 你会在这里得到异常

【讨论】:

    【解决方案2】:

    -由于每个线程都有一个 obj 的副本,我刚刚得到 ​​p>

    1
    1
    2
    

    作为输出

    --我修改了程序,让线程 Child1 和 Child2 共享对象。

    package multi_threading;
    
     public class inter_thread {
        Thread t;
        public inter_thread(test_value obj,String msg){
            t=new Thread(obj,msg);
            t.start();
        }
    
    }
    
     class test_value implements Runnable{
         boolean val=false;
        public static void main(String args[]){
            test_value obj=new test_value();
            inter_thread obj1=new inter_thread(obj,"Child1"); 
            inter_thread obj2=new inter_thread(obj,"Child2");
            try{
                obj1.t.join();
                obj2.t.join();
            }catch(InterruptedException e){
                System.out.println("Interrupted");
            }
        }
        public void run(){
            int i;
            synchronized(obj){
            for(i=1;i<=5;i++){
                System.out.println(i);
                obj.val=!obj.val;
                while(obj.val)
                    try{
                        wait();
                    }catch(InterruptedException e){
                        System.out.println("Interrupted");
                    }
                   notify();
               }
           }
        }
     }
    

    -我收到一个编译错误,提示无法解析 obj。

    -如何使obj在线程之间共享以及如何产生输出如下:

    1
    1
    2
    2
    3
    3
    4
    4
    5
    5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多