नहीं , एक कंस्ट्रक्टर सिंक्रनाइज़ नहीं किया जा सकता जावा में। जेवीएम यह सुनिश्चित करता है कि एक निश्चित समय में केवल एक थ्रेड कंस्ट्रक्टर कॉल को कॉल कर सकता है। इसीलिए कंस्ट्रक्टर को सिंक्रनाइज़्ड और . घोषित करने की आवश्यकता नहीं है यह अवैध हैl जावा में। हालांकि, हम सिंक्रनाइज़ किए गए ब्लॉक . का उपयोग कर सकते हैं एक कंस्ट्रक्टर के अंदर।
यदि हम एक सिंक्रनाइज़ . डालने का प्रयास कर रहे हैं कीवर्ड एक कंस्ट्रक्टर से पहले, कंपाइलर कहता है कि "त्रुटि:संशोधक तुल्यकालन की यहां अनुमति नहीं है ".
उदाहरण
public class SynchronizedConstructorTest {
// declaration of synchronized constructor
public synchronized SynchronizedConstructorTest() {
System.out.println("Synchronized Constructor");
}
public static void main(String args[]) {
SynchronizedConstructorTest test = new SynchronizedConstructorTest();
}
} आउटपुट
SynchronizedConstructorTest.java:3: error: modifier synchronized not allowed here
public synchronized SynchronizedConstructorTest() {
^
1 error