从Java到.net的转换问题 [英] Conversion issue from Java to .net

查看:97
本文介绍了从Java到.net的转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在将java calss转换为.net类,但我确实坚持下面的内容.我用.net编写的类是否可以等效于Java类,有人可以向我提出建议吗?如果没有,请给我您的建议,这对我真的有帮助.


JAVA:
-----

Hi,
I''m converting java calss to .net class, i''m really stuck with the below part. Can any one suggest to me if the class I''ve written in .net is the valid equivalent for the java class? If not please give me your suggestions, that would really help me.


JAVA:
-----

public class QueueMonitor implements Runnable
{
    public void run() 
    {
        while(true) 
        {
        }
    }
}


.Net:
----


.Net:
----

public class QueueMonitor
{
    public QueueMonitor(string strTest)
    {
        Thread thread = new Thread(Run);
        thread.Start();
    }
    public void Run()
    {
        while (true)
        {
        }
    }
}



谢谢,
Karthik



Thanks,
Karthik

推荐答案

您不应该具有线程.在构造函数中启动.我认为这将是适当的:

You should not have thread.Start in the constructor. I think this will be appropriate:

public class QueueMonitor{    

public void BeginQueueMonitor(string strTest){        
Thread thread = new Thread(Run);
thread.Start();
}    

public void Run(){
while (true){
}
}
}



然后,调用方法应创建此类的实例,并调用BeginQueueMonitor以启动线程.

顺便说一句,您真的需要无限循环吗?



Calling method should then create an instance of this class and call BeginQueueMonitor to start the thread.

BTW do you really need an infinite loop?


我不太确定为什么在构造对象时已经启动了线程.另一件事是,您应该在类的私有成员字段中跟踪创建的线程,以便例如当您想要停止队列监视器并且线程构造函数需要ThreadStart实例时可以访问它. > 您的构造函数还具有一个变量1中未使用的字符串参数.如果您的Run方法将使用String参数,那么您也可以传递该变量(variant2).

I''m not quite sure why you already start the thread when the object is constructed. Another thing is that you should keep track of the created thread in a private member field in your class, so that you have access to it for instance when you want to stop your queue monitor and the thread constructor wants a ThreadStart instance.
Your constructor also has a string parameter which isn''t used in variant 1. If your Run method would take a String parameter you could pass that (variant2) too.

public class QueueMonitor
{
    private Thread queueMonitorThread1;
    private Thread queueMonitorThread2;
    private String myString;

    public QueueMonitor(String strTest)
    {
         this.myString = strTest;
    }

    public StartQueueMonitor(string strTest)
    {
        queueMonitorThread1 = new Thread(new ThreadStart(Run1));
        queueMonitorThread1.Start();

        queueMonitorThread2 = new Thread(new ParameterizedThreadStart(Run2));
        queueMonitorThread2.Start(strTest);
    }

    public void Run1()
    {
        while (true)
        {
            //Do something here
        }
    }

    public void Run2(String test)
    {
        while(true)
        {
            //Do something different here
        }
    }
}


Java的
Java''s Runnable interface documentation[^] doesn''t require (of course) the class running the thread, hence, I suppose, you''ve to drop the .NET class ctor content. Moreover, I suppose, you have to explicitely define the .NET Runnable interface and make the QueueMonitor class implement it.


这篇关于从Java到.net的转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆