how to synchronize?

Posted on 2005-04-11
Last Modified: 2010-03-31
How to synchronize the test method here? The output prints "count is not equlat to count1" . Why is it happening?

public class t1 extends Thread {
int count = 0;

int count1 = 0;

public void run() {
System.out.println("Thread t1 started ............");
while (true) {
if (count != count1) {
System.out.println("count is not equlat to count1" + count
+ " " + count1);

public synchronized void test() {
try {
} catch (InterruptedException e) {
System.out.println(count + " " + count1);

public class threadMain extends Thread {
public static void main(String str[]) {
threadMain tt = new threadMain();
//Start this thread
threadMain() {
//Start t1 thread
tt1 = new t1();

public void run() {
System.out.println("Thread Main started ............");
while (true) {
//This thread is calling test() method of t1
//t1 is thread by itself and it is also calling test()
public t1 tt1;
Question by:potiyan
    LVL 92

    Accepted Solution

    looks like you also want to synchronize the if statement:

    synchronized (this)
       if (count != count1) {
          System.out.println("count is not equlat to count1" + count+ " " + count1);
    LVL 30

    Expert Comment

    I don't think that would help because count and count1 are not being modified anywhere else but the test () method. And control would reach the if () statement only after the test () method has executed.

    It looks the code that has been posted will not even compile. Can you post the actual code you are using?
    LVL 30

    Expert Comment

    Oh, already closed :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now