【问题标题】:OSGI bundle and threadsOSGI 包和线程
【发布时间】:2015-02-22 19:11:54
【问题描述】:

我是 OSGI 的新手,我有当前的目标。我有 10 个线程,它将它们的名称写在一个文件中。记录线程随机休眠 0..1 秒后。这一切都必须是一个捆绑包。我创建它,但我不确定这是否正确。任何cmets都可以吗?

package helloworld;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import writer.StartThreads;
public class Activator implements BundleActivator {
    public void start(BundleContext context) throws Exception {
        System.out.println("Start Thred!!");

        new StartThreads().Execute();
    }

    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye World!!");
    }
}

1

package writer;

import writer.WriterLogs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class StartThreads {

    public static void Execute() {
        BufferedWriter writer = null;
        File textFile = new File("threadLog.txt");
        // if file doesnt exists, then create it
        if (!textFile.exists()) {
            try {
                textFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            writer = new BufferedWriter(new FileWriter(textFile, true));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < 10; i++) {
            WriterLogs wrt = new WriterLogs(writer);
            Thread worker = new Thread(wrt);

            worker.setName("Nisha-" + i);
            worker.start();
            try {
                worker.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        try {
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

2

package writer;

import java.io.BufferedWriter;
import java.io.IOException;

public class WriterLogs implements Runnable {

    private BufferedWriter writer;

     public WriterLogs(BufferedWriter wr) {
        this.writer = wr;
    }

    @Override
    public void run() {
         try           
         {   
             try {
                 synchronized(this.writer) {
                 this.writer.write(Thread.currentThread().getName() + "\n"); 
                 }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             // set random 0...1 s.
             Thread.sleep((long)(Math.random() * 1000));
             System.out.println(Thread.currentThread().getName());  
         }         
         catch (InterruptedException interruptedException)         
         {             
            /*Interrupted exception will be thrown when a sleeping or waiting                
             * thread is interrupted.                
             */                
             System.out.println( Thread.currentThread().getName() +interruptedException);            
         } 
    }

}

【问题讨论】:

  • 如果此代码尽您所知,并且您想了解改进,您可以将其发布到Code Review
  • 我会说你必须关闭stop上的所有捆绑资源。目前你的线程应该自己死掉,但在一般情况下你不能认为这是理所当然的。
  • @蜘蛛鲍里斯 你说我什么?看我关闭文件。
  • 假设我 start 捆绑并立即 stop 它,会发生什么?
  • 嗯,这个问题的决定是什么?

标签: java multithreading osgi osgi-bundle


【解决方案1】:

这是不正确的。就像蜘蛛 Boris 所说,当你的包停止时,你应该释放所有资源并停止包正在进行的任何处理。因此,从stop 方法中,您应该以某种方式通知您的线程尽快停止。

在实践中,您可能不会让代码运行,但这绝对不是您应该在 OSGi 中编写代码的方式(这就是您所要求的)。

【讨论】:

  • 是的。您的代码应该尽可能快地执行此操作,并且 stop 方法应该等待它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 2012-06-26
  • 2015-05-22
相关资源
最近更新 更多