Java线程同步——synchronized关键字的放置

标签 java multithreading

我正在尝试使用 synchronized 关键字来同步线程应用程序。它有时表现得很奇怪。关键字 synchronized 的位置存在歧义。代码如下:

class Athread implements Runnable
{
    public void run() {

            System.out.println("Starting Implementation of Class Athread");
            for(int i=1;i<=10;i++)
            {
                System.out.println("Class Athread :"+i);
            }
            System.out.println("Ending Implementation of class Athread");
    }
}

class Bthread implements Runnable
{
    public void run() {

            System.out.println("Starting Implementation of Class Bthread");
            for(int i=11;i<=20;i++)
            {
                System.out.println("Class Bthread :"+i);
            }
            System.out.println("Ending Implementation of class Bthread");
    }
}

public class ThreadDemo {
    public static void main(String[] args) {
        System.out.println("Program starts..");

        Athread t1 = new Athread();
        Thread th1 = new Thread(t1);


        Bthread t2 = new Bthread();
        Thread th2 = new Thread(t2);

        synchronized(th1){
            th1.start();
        }
        synchronized(th2){
            th2.start();
        }

        System.out.println("Program ends...");
    }
}

当我将同步块(synchronized block)放在线程的开头时,它应该锁定该线程而不与其他线程交错。可悲的是,输出并不像预期的那样。我的输出如下:

Program starts..
Program ends...
Starting Implementation of Class Athread
Starting Implementation of Class Bthread
Class Athread :1
Class Bthread :11
Class Athread :2
Class Bthread :12
Class Athread :3
Class Bthread :13
Class Athread :4
Class Bthread :14
Class Athread :5
Class Bthread :15
Class Athread :6
Class Bthread :16
Class Athread :7
Class Bthread :17
Class Athread :8
Class Bthread :18
Class Athread :9
Class Bthread :19
Class Athread :10
Class Bthread :20
Ending Implementation of class Athread
Ending Implementation of class Bthread

即使我在两个类的 run() 方法中使用同步关键字,我也没有得到实际需要的输出。

谁能解释一下如何将 synchronized 关键字应用于我上面的示例。

谢谢。

更新:

在上面的问题中,我错误地理解了同步的概念。根据收到的评论,我正在尝试调用两个不同的线程并访问两个不同的资源。但是应该在共享/公共(public)资源上进行同步。因此,应该有一个我应该尝试同步的公共(public)位置。

最佳答案

您所做的是,在您的线程开始处同步了一段代码。那时只有 main 方法被执行,所以放置同步块(synchronized block)你什么也没做。

第二件事是你在 2 个不同的对象上创建 2 个线程,所以两个线程都不会相互干扰,所以将同步块(synchronized block)放在 run 中或同步 run 方法不会做任何不同的事情。

我不知道你到底想达到什么目的,但我修改了你的“同步”示例代码。

class MyClass implements Runnable{
    public void run() {
            System.out.println(this);
            System.out.println("Starting Implementation of "+Thread.currentThread().getName());
            synchronized(this){
                for(int i=1;i<=10;i++){
                    System.out.println("Class  :"+Thread.currentThread().getName()+" "+i);
                }
            }
            System.out.println("Ending Implementation of "+Thread.currentThread().getName());
    }
}


public class ThreadDemo {
    public static void main(String[] args) {
        System.out.println("Program starts..");

        MyClass obj = new MyClass();
        Thread th1 = new Thread(obj);
        Thread th2 = new Thread(obj);
        th1.setName("Athread");
        th2.setName("BThread");
        th1.start();
        th2.start();

        System.out.println("Program ends...");
    }
}

关于Java线程同步——synchronized关键字的放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20342363/

相关文章:

java.lang.ClassNotFoundException : com. jweb.beans.News

windows - 在 Media Foundation 中使用 COINIT_APARTMENTTHREADED 或 COINIT_MULTITHREADED?

android - 如何在 Android 中暂停/恢复线程?

android - 处理程序和多个 Activity

c# - 多线程数组线程安全吗?

java - 如何处理 TextView 中使用字符串资源的2个链接点击?

java - 无法从Object转换为IntWritable

spring - 如何在执行许多异步后台任务时保持网络服务器响应

java - 如何在 GWT 中使用非静态方法@Using window.addEventListener ('message' ~

java - 在Java中,如何为FileChannel设置进度条?