Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it might cause multithread problem i think.. #8

Open
SR1s opened this issue Mar 23, 2015 · 3 comments
Open

it might cause multithread problem i think.. #8

SR1s opened this issue Mar 23, 2015 · 3 comments

Comments

@SR1s
Copy link

SR1s commented Mar 23, 2015

I saw that className and methodName variates is static,
that means when using this class on multithread condition,
it will be incorrect if there is loging too much

@MustafaFerhan
Copy link
Owner

Hi @SR1s

What's the difference between DebugLog and Log.java?
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/util/Log.java

@SR1s
Copy link
Author

SR1s commented Mar 27, 2015

Ok, the major difference is that you add extra message like class name, function name and line number. It's a good feature for debug program. But the problem is that you store these extra message in the static variate, in multithread context, it will be change by other thread when it is loging one log but interrupt by the VM.

Let me show you a example.

I did a test, the code is below, using DebugLog to log the message.

public class App extends Application {

    private static Context sContext;

    @Override
    public void onCreate() {
        super.onCreate();

        Thread t1 = new LogOne();
        Thread t2 = new LogTwo();
        t1.start();
        t2.start();

    }

    public static Context getContext() {
        return sContext;
    }


    class LogOne extends Thread {

        @Override
        public void run() {
            super.run();
            int i = 10000;
            while (i-- > 0) {
            messageOne();
            }
        }

        private void messageOne() {
            DebugLog.i("log by LogOne");
        }
    }

    class LogTwo extends Thread {

        @Override
        public void run() {
            super.run();
            int i = 10000;
            while (i-- > 0) {
                messageTwo();
            }
        }

        private void messageTwo() {
            DebugLog.i("log by LogTwo");
        }
    }

}

It should always only log these two message:

  1. [messageOne:41]log by LogOne
  2. [messageTwo:57]log by LogTwo

However, in the logcat, I saw this:
error output
error output2

This is what I mean the multithread problem.

My suggestion is using local variate instead of static variate to avoid these problem.

@davideas
Copy link

davideas commented Jun 4, 2017

Another simpler suggestion is to create local variable or private function that returns the tag and the line number, also because every log line is different of the previous, let's say, at 99% of the times. So why keep them in a static variable if they are changed at every call?

I've created a new Log class based on the roboguice/util/Ln.java class and this one. This class resolves also the issue #10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants