From 418503275c52dba03c1c9dffec8aabfacd33421e Mon Sep 17 00:00:00 2001 From: perkj Date: Mon, 5 Oct 2015 20:49:04 +0200 Subject: [PATCH] Add ThreadChecker class to ThreadUtils This class can be used for checking that method calls are made on the correct thread. R=magjed@webrtc.org Review URL: https://codereview.webrtc.org/1384303002 . Cr-Commit-Position: refs/heads/master@{#10173} --- .../java/android/org/webrtc/ThreadUtils.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java b/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java index e7dc331103..5e6cc52895 100644 --- a/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java +++ b/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java @@ -30,6 +30,26 @@ package org.webrtc; import java.util.concurrent.CountDownLatch; final class ThreadUtils { + /** + * Utility class to be used for checking that a method is called on the correct thread. + */ + public static class ThreadChecker { + private Thread thread = Thread.currentThread(); + + public void checkIsOnValidThread() { + if (thread == null) { + thread = Thread.currentThread(); + } + if (Thread.currentThread() != thread) { + throw new IllegalStateException("Wrong thread"); + } + } + + public void detachThread() { + thread = null; + } + } + /** * Utility interface to be used with executeUninterruptibly() to wait for blocking operations * to complete without getting interrupted..