线程八锁以及代码实现

题目:判断打印的 “one” or “two” ?

    1. 两个普通同步方法,两个线程,标准打印, 打印? //one two
    1. 新增 Thread.sleep() 给 getOne() ,打印? //one two
    1. 新增普通方法 getThree() , 打印? //three one two
    1. 两个普通同步方法,两个 Number 对象,打印? //two one
    1. 修改 getOne() 为静态同步方法,打印? //two one
    1. 修改两个方法均为静态同步方法,一个 Number 对象? //one two
    1. 一个静态同步方法,一个非静态同步方法,两个 Number 对象? //two one
    1. 两个静态同步方法,两个 Number 对象? //one two

// 1. 两个普通同步方法,两个线程,标准打印, 打印? //one two
// 2. 新增 Thread.sleep() 给 getOne() ,打印? //one two


public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getTwo();
            }
        }).start();
    }

}
class Demo{
    public synchronized void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized void getTwo(){
        System.out.println("two");
    }
}

1、2:同步方法的所对象是this,先调用的方法先执行,虽然是两个线程,但是只能有一个同步方法被执行。

// 3. 新增普通方法 getThree() , 打印? //three one two

public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getTwo();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();
    }

}
class Demo{
    public synchronized void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized void getTwo(){
        System.out.println("two");
    }
    public void getThree(){
        System.out.println("Three");
    }
}

3.同步方法不受锁的约束

 // 4. 两个普通同步方法,两个 Number 对象,打印?  //two  one
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        Demo demo1=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo1.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

4.形成线程同步的前提是,多个线程拥有同一把锁,本题创建了两个对象,相当于两把锁了。故:各自执行各自的

//5. 修改 getOne() 为静态同步方法,打印?  //two   one
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized static void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

5.静态的同步方法的锁是类实例,同步方法的锁是this,两个线程拥有不同的锁,所以实现不了同步

// * 6. 修改两个方法均为静态同步方法,一个 Number 对象?  //one   two
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized static void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized static void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

6.都是静态方法,公用一个对象锁(类实力),故可以实现同步

//* 7. 一个静态同步方法,一个非静态同步方法,两个 Number 对象?  //two  one
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        Demo demo1=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo1.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized static void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized  void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

7.不是同一个锁,故不同步

// * 8. 两个静态同步方法,两个 Number 对象?   //one  two
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        Demo demo1=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo1.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized static void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized static void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

8.静态同步方法的锁是“类实例”,与多少个对象无关

总结:通过上述的代码,可以得出:

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C语言中可以使用pthread库来实现线程编程,其中pthread_mutex_t类型变量可以用来实现线程锁。 示例代码: ``` #include <pthread.h> pthread_mutex_t lock; void some_function() { pthread_mutex_lock(&lock); // critical section pthread_mutex_unlock(&lock); } int main() { pthread_mutex_init(&lock, NULL); // create and start threads pthread_mutex_destroy(&lock); return 0; } ``` 在程序开始时,使用pthread_mutex_init()函数初始化互斥锁。在需要保护的临界区域内调用pthread_mutex_lock()函数获得锁,在临界区域外调用pthread_mutex_unlock()释放锁。在程序结束时,使用pthread_mutex_destroy()销毁互斥锁。 需要注意的是,在获得锁后,如果另一个线程尝试获得相同的锁,它将会被阻塞直到锁被释放。 ### 回答2: 实现线程的并发的线程锁的C代码通常使用互斥锁(Mutex)或者读写锁(ReadWrite Lock)来实现。 互斥锁的实现如下: ```c #include <stdio.h> #include <pthread.h> int count = 0; pthread_mutex_t lock; void* increment(void* arg) { pthread_mutex_lock(&lock); // 加锁 count++; pthread_mutex_unlock(&lock); // 解锁 return NULL; } int main() { pthread_t thread1, thread2; pthread_mutex_init(&lock, NULL); // 初始化互斥锁 pthread_create(&thread1, NULL, increment, NULL); pthread_create(&thread2, NULL, increment, NULL); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_mutex_destroy(&lock); // 销毁互斥锁 printf("Count: %d\n", count); // 输出结果 return 0; } ``` 读写锁的实现如下: ```c #include <stdio.h> #include <pthread.h> int count = 0; pthread_rwlock_t rwlock; void* read_func(void* arg) { pthread_rwlock_rdlock(&rwlock); // 读取加锁 printf("Read: %d\n", count); // 读取共享变量 pthread_rwlock_unlock(&rwlock); // 解锁 return NULL; } void* write_func(void* arg) { pthread_rwlock_wrlock(&rwlock); // 写入加锁 count++; // 修改共享变量 pthread_rwlock_unlock(&rwlock); // 解锁 return NULL; } int main() { pthread_t read_thread, write_thread; pthread_rwlock_init(&rwlock, NULL); // 初始化读写锁 pthread_create(&read_thread, NULL, read_func, NULL); pthread_create(&write_thread, NULL, write_func, NULL); pthread_join(read_thread, NULL); pthread_join(write_thread, NULL); pthread_rwlock_destroy(&rwlock); // 销毁读写锁 return 0; } ``` 以上代码分别使用互斥锁和读写锁来保证对共享变量的并发访问的线程安全。 ### 回答3: 多线程的并发中,为了保证线程间数据的一致性和安全性,需要使用线程锁。 在C语言中,可以使用互斥锁(mutex)来实现线程锁。以下是一个简单的使用互斥锁的多线程并发的C代码示例: 首先,需要引入头文件"pthread.h",这是C语言中用于多线程编程的标准库。 ```c #include <pthread.h> #include <stdio.h> // 定义全局变量和互斥锁 int count = 0; pthread_mutex_t lock; // 线程函数 void* thread_func(void* arg) { // 加锁 pthread_mutex_lock(&lock); // 临界区操作 count++; printf("Thread ID: %ld, Count: %d\n", pthread_self(), count); // 解锁 pthread_mutex_unlock(&lock); // 线程退出 pthread_exit(NULL); } int main() { // 初始化互斥锁 pthread_mutex_init(&lock, NULL); // 创建多个线程 pthread_t tid1, tid2; pthread_create(&tid1, NULL, thread_func, NULL); pthread_create(&tid2, NULL, thread_func, NULL); // 等待线程结束 pthread_join(tid1, NULL); pthread_join(tid2, NULL); // 销毁互斥锁 pthread_mutex_destroy(&lock); return 0; } ``` 上述代码中,定义了一个全局变量`count`用于记录操作次数,并且定义了互斥锁`lock`。 线程函数`thread_func()`中,在临界区操作前先加锁,操作完成后解锁。这样就保证了同时只有一个线程能够进入临界区,保证了数据的一致性和安全性。 在`main()`函数中,首先初始化互斥锁,然后创建多个线程并等待线程结束。最后销毁互斥锁。 这样,通过互斥锁的加锁和解锁操作,实现了多线程的并发中线程锁的机制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值