小编ada*_*ada的帖子

观察者可以安全地用Rx监听多个可观测量吗?

我试图看看如何使用Rx将多个可观察事件流式传输到一组事件中.但是当我运行下面的代码时,我得到一个例外.那么这是否意味着由于违反Rx语法,多个观察者总是容易出现异常?我的意思是如果这些多个观察者中的两个偶然同时生成一个事件(任何两个可观察者将同时产生一些概率),它应该给出一个例外.

DateTimeOffset start;
        object sync = new object();
        var subject = new Subject<long>();
        var observer = Observer.Create<long>(c =>
        {
            lock (sync)
            {
                Console.WriteLine(c);
            }
        })
            ;

        var observable1 = Observable.Interval(TimeSpan.FromSeconds(2));
        var observable2 = Observable.Interval(TimeSpan.FromSeconds(5));
        var observable3 = Observable.Never<long>().Timeout
            (start = DateTimeOffset.Now.AddSeconds(15),
             (new long[] { 1 }).ToObservable());
        var observable4 = Observable.Never<long>().Timeout(start);
        observable1.Subscribe(observer);
        observable2.Subscribe(observer);
        observable3.Subscribe(observer);
        observable4.Subscribe(observer);
        Thread.Sleep(20000);
Run Code Online (Sandbox Code Playgroud)

感谢Gideon的解释.这是我得到的例外.你是对的,这是一个时间的例子.这是一个编码错误.谢谢.

System.TimeoutException: The operation has timed out.
   at System.Reactive.Observer.<Create>b__8[T](Exception e)
   at System.Reactive.AnonymousObserver`1.Error(Exception exception)
   at System.Reactive.AbstractObserver`1.OnError(Exception error)
   at System.Reactive.Subjects.Subject`1.OnError(Exception error)
   at System.Reactive.AnonymousObservable`1.AutoDetachObserver.Error(Exception e
xception)
   at System.Reactive.AbstractObserver`1.OnError(Exception …
Run Code Online (Sandbox Code Playgroud)

c# system.reactive observable observer-pattern

4
推荐指数
1
解决办法
3619
查看次数

私有静态变量是必需的公共setter吗?

/* Atleast is it needed in this case?*/
 public class Service
{       
         private static List<String> subscribedFields;
    private static List<String> unsubscribedFields;
         //---------------------------------  
         // is this necessary?
         //---------------------------------
    public static void setFields(List<String> subscribedFields, List<String> unsubscribedFields)
    {
        Service.subscribedFields = subscribedFields;
        Service.unsubscribedFields = unsubscribedFields;
    }
    public static List<String> getSubscribedFields()
    {
        return subscribedFields;
    }
    public static List<String> getUnsubscribedFields()
    {
        return unsubscribedFields;
    }
}
// some other class
public class User{
     // Is this not enough to change the lists? Isn't the setter redundant? 
     Change(Service.getSubscribedFields());
     Change(Service.getUnsubscribedFields()); …
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
1
解决办法
4152
查看次数

是否可以在Rx中的不同线程上调用订阅者的OnNexts?

我是Rx的新手.我想知道是否可以向不同的订阅者发送消息,以便它们在不同的线程上运行?IObserable如何控制呢?简单的Subject实现,据我所知,它在一个线程上一个接一个地调用订阅者.


public class Subsciber : IObserver<int>
{
    public void OnNext(int a)
    {
        // Do something
    }
    public void OnError(Exception e)
    {
        // Do something
    }
    public void OnCompeleted()
    {
    }

} 

public static class Program
{
   public void static Main()
   {
       var observable = new <....SomeClass....>();
       var sub1 = new Subscriber();
       var sub2 = new Subscriber();
       observable.Subscribe(sub1);
       observable.Subscribe(sub2);
       // some waiting function 
   }
}
Run Code Online (Sandbox Code Playgroud)

如果我使用Subject作为'SomeClass',那么在sub1的OnNext()完成之前,不会调用sub2的OnNext().如果sub1花了很多时间,我不希望它延迟sub2的接收.有人能告诉我Rx如何为SomeClass提供这种实现.

c# system.reactive

3
推荐指数
1
解决办法
3922
查看次数

为什么我的Java swing应用程序行为不端?

当我尝试最大化窗口时,orinigal窗口渲染仍然存在,而另一个最大化窗口出现使其变得混乱.


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import javax.swing.table.TableColumnModel;


/**
 * @author ad *
 */
public class Blotter {

    private JFrame topFrame;
    private JPanel mainContentPanel;

    private JList unsubscribedFields;
    private JList subscribedFields;
    private JButton butSubscribe;
    private JButton butUnsubscribe;
    private JButton butApply;
    private JButton butOk;
    private JButton butCancel;
    private JPanel panConfirm;
    private JPanel panToggle;
    private JPanel panBottom;


    private …
Run Code Online (Sandbox Code Playgroud)

java layout swing window-resize

2
推荐指数
1
解决办法
306
查看次数

将int作为float打印时printf的行为是什么?

我在windows7上使用dev cpp来编译我的代码.

int d = 0x12;
char* e = (char*)&d;
printf("%d %d\n", sizeof (int), sizeof (float));
printf("%p %p\n", &d, (float*)&d);
printf("%p %p %p %p %p\n", &d, &e[0], &e[1], &e[2], &e[3]);
printf(" %d | %x | %#1x | %#1x | %#1x |%p\n", d,  e[0], e[1], e[2], e[3], &e[0]);
getchar();

4 4 
0028FF40 0028FF40
0028FF40 0028FF40 0028FF41 0028FF42 0028FF43  
18 | 12 | 0 | 0 | 0 |0028FF40
Run Code Online (Sandbox Code Playgroud)

你看,如果我使用%d打印d,它打印4个字节的精细.但是,如果我使用下面的%f,它会在必须打印e的第一个字节的位置显示零.任何人都可以帮助解决为什么会这样 为什么e的内容取决于d的格式?

int d = 0x12;
char* e = (char*)&d;
printf("%d %d\n", sizeof …
Run Code Online (Sandbox Code Playgroud)

c floating-point int printf casting

1
推荐指数
1
解决办法
2539
查看次数