java - OSGI 包和线程

标签 java multithreading osgi osgi-bundle

我是 OSGI 的新手,我有当前的目标。我有 10 个线程,它正在将它们的名字写在一个文件中。记录线程 sleep 随机 0..1 秒后。这一切都必须是一个包。我创建了它,但我不确定这是否正确。有什么意见吗?

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);            
         } 
    }

}

最佳答案

这是不正确的。就像 Boris the Spider 所说的那样,当您的 bundle 停止时,您应该释放所有资源并停止 bundle 正在进行的任何处理。因此,在 stop 方法中,您应该以某种方式向您的线程发出信号,让它们尽快停止。

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

关于java - OSGI 包和线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28662058/

相关文章:

java - 通过 SMTP Gmail Oauth2 问题发送邮件

Java OSGi : InstantiationException with Declarative Services

java - 从命令行调用 OSGi 服务

java - 可观察列表转换/转换

java - 我必须从数据库中获取 500K 行并将该数据写入文件,意味着执行 I/O 操作

.net - Android Xamarin 应用程序中 System.Threading.Tasks.RangeWorker.FindNewWork 中的 NullReferenceException

vb.net - 在多个线程中运行不同的进程而不会在VB.NET 2中重叠

java - 如何在 CQ 中从 Java 读取 OSGI 工厂配置的单个实例

带有 JPA + Hibernate(或类似)和 Apache Derby 嵌入式数据库的 Java 独立应用程序

android - Surfaceview或View for Puzzle应用