【问题标题】:How to add item to list using a thread如何使用线程将项目添加到列表
【发布时间】:2018-05-12 19:01:31
【问题描述】:

我有一个预订清单,但有些有冲突(他们是在同一天预订了同一个房间)。为了解决这个问题,我想创建一个没有冲突的新列表。我只能更改房间号,不能更改预订日期,所以我通过以下方法进行:

        public List<Reservation> ResolveConflicts()
        {
            var groupedReservationByDate = _reservations.GroupBy(reservation => reservation.CheckInDate);
            var resolvedReservations = new List<Reservation>();
            foreach (var reservations in groupedReservationByDate)
            {
                foreach (var currentReservation in reservations)
                {
                    var nextReservations = reservations.Skip(reservations.ToList().IndexOf(currentReservation) + 1)
                        .ToList();
                    foreach (var nextReservation in nextReservations)
                    {
                        if (currentReservation.RoomNumber == nextReservation.RoomNumber)
                        {
                            var thread = new Thread(() => ChangeRoomNumber(reservations, currentReservation));
                            thread.Start();
                        }
                    }

                    resolvedReservations.Add(currentReservation);
                }
            }

            return resolvedReservations;
        }

        private void ChangeRoomNumber(IEnumerable<Reservation> reservations, Reservation currentReservation)
        {
            var roomNumber = GetRoomNumber();
            foreach (var reservation in reservations)
            {
                if (roomNumber == reservation.RoomNumber)
                    roomNumber = GetRoomNumber();
            }

            currentReservation.RoomNumber = roomNumber ;
        }

        private int GetRoomNumber()
        {
            var randomNumber = new Random();
            return randomNumber.Next(1, _reservations.MaxBy(reservation => reservation.RoomNumber).RoomNumber);
        }

当我使用线程执行此操作时,我会得到一种正确的列表,但不正确,返回的列表应该没有冲突。当我使用 Thread.Sleep(1000) Console output with reservation conflicts using a thread 时会产生相同的输出。

如果我不使用线程,返回的列表是完全不正确的,同一个房间有很多冲突Console output with reservation conflicts without using a thread

我认为是线程有问题,算法是正确的或者至少我希望是正确的。

【问题讨论】:

  • And if I don't use a thread, the returned list is totally incorrect, for the same room are a lot of conflicts 如果你在没有使用多线程的情况下得到不正确的结果,那么这显然意味着你的算法不正确。我不明白你为什么认为添加多线程会改善这种情况
  • 不看很远,你的ChangeRoomNumber 方法就坏了。你得到一个随机的房间号,然后遍历预订并生成一个新的房间号,如果之前的房间号已经被分配。这是错误的,如果您生成一个新号码,那么您必须重新检查所有预订
  • 谢谢!就是这个问题,我改了房间号,但是我又要检查一下是否还有其他冲突。

标签: c# multithreading list dictionary


【解决方案1】:

感谢 Kevin,我已经解决了这个问题:

没有看得很远,你的 ChangeRoomNumber 方法就坏了。你得到一个随机的房间号,然后遍历预订并生成一个新的房间号,如果之前的房间号已经被分配。这是错误的,如果您生成一个新号码,那么您必须重新检查所有预订 - @Kevin Gosse

解决办法是:

private void ChangeRoomNumber(List<Reservation> reservations, Reservation currentReservation)
{
    while (IsConflict(reservations, currentReservation))
        currentReservation.RoomNumber = GetRoomNumber();
}

private bool IsConflict(IEnumerable<Reservation> reservations, Reservation currentReservation)
{
    return reservations.Any(reservation =>
        !ReferenceEquals(reservation, currentReservation) &&
        reservation.RoomNumber == currentReservation.RoomNumber);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多