BUG:The channel 'tcp' is already registered.

稳定重现bug的程序:

class Program
    {
        static void Main(string[] args)
        {
            System.Threading.Thread[] threads = new System.Threading.Thread[2];
            threads[0] = new System.Threading.Thread(new ThreadStart(GetConnectionManager));
            threads[1] = new System.Threading.Thread(new ThreadStart(GetConnectionManager));
            threads[0].Start();
            threads[1].Start();
            
            Console.ReadKey();
        }
       
       

        static private void GetConnectionManager()
        {
            lock (typeof(Program))
            {
                bool hasTcpChannel = false;
                foreach (var item in ChannelServices.RegisteredChannels)
                {
                    if (item is TcpClientChannel)
                    {
                        hasTcpChannel = true;
                    }
                }

                if (!hasTcpChannel)
                {
                
                    IChannel clientChannel = new TcpClientChannel();
                    Thread.Sleep(2000);
                    ChannelServices.RegisterChannel(clientChannel, false);
                }
            }
        }
    }


 

我们的解决方案是:

class Program
    {
        static void Main(string[] args)
        {
            System.Threading.Thread[] threads = new System.Threading.Thread[2];
            threads[0] = new System.Threading.Thread(new ThreadStart(GetConnectionManager));
            threads[1] = new System.Threading.Thread(new ThreadStart(GetConnectionManager));
            threads[0].Start();
            threads[1].Start();
            
            Console.ReadKey();
        }
      
       

        static private void GetConnectionManager()
        {
            //加锁来保证线程安全
            lock (typeof(Program))
            {
                bool hasTcpChannel = false;
                foreach (var item in ChannelServices.RegisteredChannels)
                {
                    if (item is TcpClientChannel)
                    {
                        hasTcpChannel = true;
                    }
                }

                if (!hasTcpChannel)
                {
                
                    IChannel clientChannel = new TcpClientChannel();
                    Thread.Sleep(2000);
                    ChannelServices.RegisterChannel(clientChannel, false);
                }
            }
     
    }


 

如有更好地建议,请留言!谢谢!

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sts报错,报错信息如下,如何确认问题:06-09 14:59:32 I/ModuleListener: [1/1] EQ5T45G66XCAAQMN com.android.compatibility.common.tradefed.testtype.JarHostTest android.security.sts.Bug_258188673#testPocBug_258188673 FAILURE: java.lang.AssertionError: PoC exited with bad exit code.. Actual: 113 at org.junit.Assert.fail(Assert.java:89) at org.junit.Assert.failEquals(Assert.java:187) at org.junit.Assert.assertNotEquals(Assert.java:201) at com.android.sts.common.NativePocStatusAsserter$1.checkCmdResult(NativePocStatusAsserter.java:32) at com.android.sts.common.NativePoc.runPocAndAssert(NativePoc.java:264) at com.android.sts.common.NativePoc.run(NativePoc.java:224) at android.security.sts.Bug_258188673.testPocBug_258188673(Bug_258188673.java:38) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:61) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值