Android如何比较日期类型变量? [英] Android how to compare date type variables ?

查看:79
本文介绍了Android如何比较日期类型变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较保存在字符串变量中的日期吗?

I need to compare to dates saved in string variables ?

推荐答案

你的计算方法不一样。



起始值

Your calculations are not identical.

The start value
estimate = (f(a) + f(b)) / 2.0;



在每个线程中设置。所以它被添加 n 次。这应该在开始线程之前分配一次。



最终计算


is set in each thread. So it is added n times. This should be assigned once before starting the threads.

The final calculation

estimate = estimate * h * 4;



也在每个线程结束时执行。这应该在所有线程完成后在main函数中完成。



在单线程版本中,您将从1迭代到 n 。但是在你的第一个线程中你从0开始。所以你在结果中加1(对于x == 0,f(x)== 1)。



因此,您的主要功能代码应如下所示:




is also performed at the end of each thread. This should be done in the main function after all threads has finished.

In your single thread version you are iterating from 1 to n. But in your first thread you are starting at 0. So you are adding 1 to the result (f(x) == 1 for x == 0).

So your main function code should look like:

// Initialise start value before starting the threads
estimate = (f(a) + f(b)) / 2.0;

for (thread = 0; thread < thread_count; thread++)
    pthread_create(&thread_handles[thread], NULL,
        Thread_sum, (void*)thread);
 
for (thread = 0; thread < thread_count; thread++)
    pthread_join(thread_handles[thread], NULL);

// Final calculation after all threads has finished.
estimate = estimate * h * 4;





并且线程函数应该是(也使用锁定来避免竞争条件):



And the thread function should be (using also locking here to avoid race conditions):

pthread_mutex_t mutexsum;

void* Thread_sum(void* rank)
{
    // ...
    // Start at 1 for first thread
    if (0 == my_first_i)
        my_first_i = 1;
    
    // estimate has been already initialised with start value
    //my_estimate = (f(a) + f(b)) / 2.0;
    
    for (i = my_first_i; i < my_last_i; i++)
    {
        x = a + i*h;
        my_estimate += f(x);
    }
    // This will be done later for estimate when all threads has finished
    //my_estimate = my_estimate *h * 4;
    
    while (flag != my_rank);
    // Lock to avoid race conditions
    pthread_mutex_lock(&mutexsum);
    estimate += my_estimate;
    flag = (flag+1) % thread_count;
    pthread_mutex_unlock(&mutexsum);
    return NULL;
}


请阅读:竞争条件 - 维基百科,免费的百科全书 [ ^ ]。



很抱歉没有修复你的bug;这可能需要相当长的时间。理解这个想法更重要。



假设你想让两个线程在同一个目标变量上运行;一个读取它并乘以2,另一个加3。如果线程独立运行,结果取决于哪个线程首先进入其工作;没有互斥体可以帮助,这是一个纯粹的逻辑。在一个案例中,你做

Please read: Race condition — Wikipedia, the free encyclopedia[^].

Sorry for not fixing your bug; it may take considerable time. It's more important that you understand the idea.

Let's say, you want have two threads operating on the same target variable; one reads it and multiplies by 2, another one adds 3 to it. If the threads run independently, the result depends on which one gets to its job first; and no mutex can help here, this is a pure logic. In one case, you do
x := (x * 2) + 3;


其他
case,它将是


in other case, it will be

x := (x + 3) * 2;



(故意,我使用伪代码符号而不是像C一样,以非模糊的方式显示赋值。)



也就是说,如果x的初始值为1,你得到5或8,取决于一些随机时间。更糟糕的是,一个案例的概率可能比另一个案例高出数百万倍(并且可能显示数十亿这样的案例:-)),因此您可以终生观察正确的结果,而不知道解决方案是完全错误的,并且当一些随机因素导致环境改变时机时可能会爆炸。



请注意竞争条件的结果(以前用于更准确和解释性的术语) 对执行时间的不正确依赖可能导致非常不同的问题,而不仅仅是不同的数字;例如,它可能导致非确定性死锁,即使竞争条件本身与死锁无关,而且几乎任何东西。多线程算法的设计方式应该是不同时序的唯一影响是时序。 :-)



-SA


这篇关于Android如何比较日期类型变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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