C#__ conflicto de acceso a recursos y problema de interbloqueo

    /// Conflicto de acceso a recursos de subprocesos: varios subprocesos solicitan un recurso al mismo tiempo, lo que provoca confusión en la lectura y la escritura.
    /// Solución: Bloquear, bloquear {segmento de programa ejecutado}: solo un subproceso puede acceder a este segmento de programa al mismo tiempo.
    /// Problema de interbloqueo:
    /// Hay demasiados bloqueos en el programa. Un determinado subproceso necesita múltiples recursos de bloqueo, y un determinado recurso está ocupado por otro subproceso, y lo mismo ocurre con otro subproceso (nadie quiere liberarlo). el recurso primero) ) forman un bucle cerrado y el hilo no puede continuar.
    /// Solución: utilice un algoritmo de programación para liberar un recurso ocupado por un subproceso o cerrar un subproceso. 

// Conflicto de acceso a recursos

    class State
    {
        private Object _lock = new Object();
        private int state = 100;
        private void test()
        {
            if(100 == state)
            {
                Console.Write("state=" + state);
            }
            state++;
        }
        private void test2()
        {
            if (101 == state)
            {
                Console.Write("state=" + state);
            }
            state++;
        }

        public void ChangeState()
        {
            Thread h = new Thread(test);
            Thread v = new Thread(test2);

            // h.Start();
            // v.Start();
            // state=100state=100state=101state=100请按任意键继续. . .

            lock (_lock)
            {
                h.Start();
                v.Start();
                // state=100请按任意键继续. . .
                // state=100state=101请按任意键继续. . .
            }
        }
    }

// programa principal

    class StateProgram
    {
        static void Main(string[] args)
        {
            State state = new State();

            for (int i = 0; i < 10; i++)
            {
                Thread t = new Thread(state.ChangeState);
                t.Start();
            }
            Thread.Sleep(1000);
        }
    }

//punto muerto

    class Deadlock
    {
        Object _lock = new Object();
        Object _lock2 = new Object();

        private int flag = 0;

        public void test()
        {
            lock (_lock)
            {
                Console.WriteLine("我拿到了锁1");
                lock (_lock2)
                {
                    Console.WriteLine("我拿到了锁2");
                    if(0 == flag)
                    {
                        Console.WriteLine("我是第一名");
                        flag = 1;
                    }
                    else
                    {
                        Console.WriteLine("我是第二名");
                    }
                }
            }
        }

        public void test2()
        {
            lock (_lock2)
            {
                Console.WriteLine("他拿到了锁2");
                lock (_lock)
                {
                    Console.WriteLine("他拿到了锁1");
                    if (0 == flag)
                    {
                        Console.WriteLine("他是第一名");
                        flag = 1;
                    }
                    else
                    {
                        Console.WriteLine("他是第二名");
                    }
                }
            }
        }
    }

// programa principal

            Deadlock star = new Deadlock();

            Thread t1 = new Thread(star.test);
            Thread t2 = new Thread(star.test2);

            t1.Start();
            t2.Start();
            //我拿到了锁1
            //他拿到了锁2

// Solución 1: bloquear la sincronización

    class Deadlock
    {
        Object _lock = new Object();
        Object _lock2 = new Object();

        private int flag = 0;

        public void test()
        {
            lock (_lock)
            {
                Console.WriteLine("我拿到了锁1");
                lock (_lock2)
                {
                    Console.WriteLine("我拿到了锁2");
                    if(0 == flag)
                    {
                        Console.WriteLine("我是第一名");
                        flag = 1;
                    }
                    else
                    {
                        Console.WriteLine("我是第二名");
                    }
                }
            }
        }

        public void test2()
        {
            lock (_lock)
            {
                Console.WriteLine("他拿到了锁2");
                lock (_lock2)
                {
                    Console.WriteLine("他拿到了锁1");
                    if (0 == flag)
                    {
                        Console.WriteLine("他是第一名");
                        flag = 1;
                    }
                    else
                    {
                        Console.WriteLine("他是第二名");
                    }
                }
            }
        }
    }

Obtuve el candado 1
Obtuve el candado 2
Fui primero
Él obtuvo el candado 2
Obtuvo el candado 1
Él fue el segundo
Presione cualquier tecla para continuar... 

// Solución 2: hacer etiquetas

    class Deadlock
    {
        Object _lock = new Object();
        Object _lock2 = new Object();

        private int flag = 0;
        private int _flag = 0;

        public void test()
        {
            if (0 == _flag)
            {
                lock (_lock)
                {
                    Console.WriteLine("我拿到了锁1");
                    lock (_lock2)
                    {
                        Console.WriteLine("我拿到了锁2");
                        if (0 == flag)
                        {
                            Console.WriteLine("我是第一名");
                            flag = 1;
                        }
                        else
                        {
                            Console.WriteLine("我是第二名");
                        }
                    }
                }
            }
            _flag = 1;
        }

        public void test2()
        {
            if(1 == _flag)
            {
                lock (_lock2)
                {
                    Console.WriteLine("他拿到了锁2");
                    lock (_lock)
                    {
                        Console.WriteLine("他拿到了锁1");
                        if (0 == flag)
                        {
                            Console.WriteLine("他是第一名");
                            flag = 1;
                        }
                        else
                        {
                            Console.WriteLine("他是第二名");
                        }
                    }
                }
            }

        }
    }
            Deadlock star = new Deadlock();
            Thread t1 = new Thread(star.test);
            Thread t2 = new Thread(star.test2);

            t1.Start();
            Thread.Sleep(1000);
            t2.Start();

Obtuve el candado 1
Obtuve el candado 2
Fui primero
Él obtuvo el candado 2
Obtuvo el candado 1
Él fue el segundo
Presione cualquier tecla para continuar...

Supongo que te gusta

Origin blog.csdn.net/qq_57233919/article/details/132766473
Recomendado
Clasificación