File: ThreadController.java

package info (click to toggle)
openjdk-11 11.0.22%2B7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,236 kB
  • sloc: java: 5,174,754; xml: 1,192,262; cpp: 1,133,036; ansic: 461,316; javascript: 162,554; sh: 16,859; objc: 13,683; python: 4,753; asm: 3,570; makefile: 2,894; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (889 lines) | stat: -rw-r--r-- 27,590 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/*
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package nsk.monitoring.share;

import java.lang.management.*;
import java.util.*;

import nsk.share.*;

/**
 * The <code>ThreadController</code> class allows to operate with threads.
 */
public class ThreadController extends StateController {

    /**
     * Type of threads: pure java.
     */
    static public final int JAVA_TYPE = 0;

    /**
     * Type of threads: native.
     */
    static public final int NATIVE_TYPE = 1;

    /**
     * Type of threads: both java and native.
     */
    static public final int MIXED_TYPE = 2;

    /**
     * Result code: no errors.
     */
    static public final int NO_ERROR = 0;

    /**
     * Result code: wrong state of the thread.
     */
    static public final int ERR_STATE = 1;

    /**
     * Result code: error in stack trace.
     */
    static public final int ERR_STACKTRACE = 2;

    /**
     * Result code: thread not found.
     */
    static public final int ERR_THREAD_NOTFOUND = 3;


    // Prefix to print while logging
    static final String LOG_PREFIX = "ThreadController> ";

    // Internal trace levels
    static final int THREAD_TRACE_LEVEL = 50;

    /**
     * Suffix of all started threads.
     */
    static final String THREAD_SUFFIX = "_ThreadMM";

    // Number of tested kinds of threads
    public static final int THREAD_KIND_COUNT = 4;

    /**
     * Index of blocked threads.
     */
    static public final int BLOCKED_IDX = 0;

    /**
     * Index of waiting threads.
     */
    static public final int WAITING_IDX = 1;

    /**
     * Index of sleeping threads.
     */
    static public final int SLEEPING_IDX = 2;

    /**
     * Index of running threads.
     */
    static public final int RUNNING_IDX = 3;

    public static final String[] THREAD_KIND_NAMES =  {"BLOCKED","WAITING","SLEEPING","RUNNABLE"};
    public static final Thread.State[] THREAD_KINDS = {Thread.State.BLOCKED, Thread.State.WAITING, Thread.State.TIMED_WAITING, Thread.State.RUNNABLE};

    private Map<Thread.State, Integer> threadsCount = new HashMap<Thread.State, Integer>();
    private Map<Thread.State, List<BaseThread>> threadsClusters = new HashMap<Thread.State, List<BaseThread>>();

    private ThreadsGroupLocks threadsGroupLocks;


    int maxDepth;
    static int invocationType;

    static {
        try {
            System.loadLibrary("ThreadController");
        } catch (UnsatisfiedLinkError e) {
            System.err.println("Could not load \"ThreadController\" "
                    + "library");
            System.err.println("java.library.path:"
                    + System.getProperty("java.library.path"));
            throw e;
        }
    }

    /**
     * Creates a new <code>ThreadController</code> object with defined
     * arguments..
     *
     * @param log            <code>Log</code> object to print info to.
     * @param threadCount    number of threads to start.
     * @param maxDepth       depth of recursion.
     * @param invocationType type of threads to start (java, native, or mixed).
     */
    public ThreadController(Log log, int threadCount, int maxDepth,
                            String invocationType) {
        logPrefix = LOG_PREFIX;
        setLog(log);
        setThreadCount(threadCount);
        setDepth(maxDepth);
        setInvocationType(invocationType);
    }


    // Calculate how many threads of each kind to start
    private void setThreadCount(int threadCount) {
        int total = 0;
        int kinds = THREAD_KIND_COUNT;
        int tmp = threadCount / kinds;
        int rest = threadCount % kinds;
        int increased = kinds - rest;
        for (int i = 0; i < kinds; i++) {
            if (i >= increased) {
                threadsCount.put(THREAD_KINDS[i], tmp + 1);
            } else {
                threadsCount.put(THREAD_KINDS[i], tmp);
            }
        }
        display("number of created threads:\t" + threadCount);
    }

    // Print thread count
    private void printThreadCount() {
        for (Thread.State state : THREAD_KINDS) {
            display("\t" + state + " threads ("
                    + threadsCount.get(state) + ")");
        }
    }

    // Set recursion depth
    private void setDepth(int depth) {
        maxDepth = depth;
        display("depth for all threads:\t" + maxDepth);
    }

    // Set invocation type
    private void setInvocationType(String value) {
        display("invocation type:\t" + value);
        if (value.equals(ArgumentHandler.JAVA_TYPE)) {
            invocationType = JAVA_TYPE;
        } else if (value.equals(ArgumentHandler.NATIVE_TYPE)) {
            invocationType = NATIVE_TYPE;
        } else if (value.equals(ArgumentHandler.MIXED_TYPE)) {
            invocationType = MIXED_TYPE;
        } else {
            throw new Failure("UNKNOWN invocation type");
        }
    }

    /**
     * Returns invocation type.
     *
     * @return invocation type.
     */
    public int getInvocationType() {
        return invocationType;
    }

    /**
     * Returns thread count.
     *
     * @param state kind of thread state
     * @return thread count.
     */
    public int getThreadCount(Thread.State state) {
        return threadsCount.get(state);
    }

      /**
     * Returns thread count.
     *
     * @param kindIndex of thread state
     * @return thread count.
     */
    public int getThreadCount(int kindIndex) {
        return threadsCount.get(THREAD_KINDS[kindIndex]);
    }

    public int getThreadKindCount() {
        return THREAD_KINDS.length;
    }


    /**
     * Brings out VM into defined state.
     * <p/>
     * The method starts all threads.
     */
    public void run() {
        long startTime = System.currentTimeMillis() / 1000;
        startThreads();
        display("locking threads");
        waitForThreads();
    }

    /**
     * Tries to return VM into initial state
     * <p/>
     * The method interrupts all threads.
     */
    public void reset() {
        for (Thread.State state : THREAD_KINDS) {
            threadsGroupLocks.releaseGroup(state);
        }
    }

    // Get thread state via JVMTI
    private native Thread.State getThreadState(Thread thread);

    // Start all threads
    private void startThreads() throws Failure {

        String tmp_name;
        BaseThread thread = null;

        threadsGroupLocks = new ThreadsGroupLocks(threadsCount, logger);
        for (Thread.State state : THREAD_KINDS) {
            threadsClusters.put(state, new ArrayList<BaseThread>());
            for (int j = 0; j < threadsCount.get(state); j++) {
                tmp_name = state + THREAD_SUFFIX + int2Str(j);
                switch (state) {
                    case BLOCKED:
                        thread = new BlockedThread(this, tmp_name, logger.getLog(), threadsGroupLocks);
                        break;
                    case WAITING:
                        thread = new WaitingThread(this, tmp_name, logger.getLog(), threadsGroupLocks);
                        break;
                    case TIMED_WAITING:
                        thread = new SleepingThread(this, tmp_name, logger.getLog(), threadsGroupLocks);
                        break;
                    case RUNNABLE:
                        thread = new RunningThread(this, tmp_name, logger.getLog(), threadsGroupLocks);
                        break;
                    default:
                        throw new TestBug("Unknow thread kind");
                }
                threadsClusters.get(state).add(thread);
                thread.start();
            }
        }
        waitForThreads();
    }

    private boolean checkState(Thread.State expectedState) {
        for (Thread thread : threadsClusters.get(expectedState)) {
            if (getThreadState(thread) != expectedState) {

                return false;
            }
        }
        return true;
    }

    private void waitForThreads() {
        for (Thread.State state : THREAD_KINDS) {
            threadsGroupLocks.waitForGroup(state);
            while (!checkState(state)) {
                Thread.yield();
            }
        }
    }


    /**
     * Finds a thread with defined id.
     *
     * @param id ID of the thread.
     * @return a thread with defined id.
     */
    public BaseThread findThread(long id) {
        for(Thread.State state:THREAD_KINDS){
            for(BaseThread thread:threadsClusters.get(state)){
                 if (id==thread.getId()) {
                     return thread;
                }
            }
        }
        return null;
    }

    /**
     * Finds a thread by name.
     *
     * @param name name of the thread.
     * @return a thread with defined name.
     */
    public BaseThread findThread(String name) {
        for(Thread.State state:THREAD_KINDS){
            for(BaseThread thread:threadsClusters.get(state)){
                 if (name.equals(thread.getName())) {
                     return thread;
                }
            }
        }
        return null;
    }

    /**
     * Checks the thread's <code>ThreadInfo</code>.
     *
     * @param info <code>ThreadInfo</code> object to test.
     * @return result code.
     * @see #NO_ERROR
     * @see #ERR_THREAD_NOTFOUND
     * @see #ERR_STATE
     * @see #ERR_STACKTRACE
     */
    public int checkThreadInfo(ThreadInfo info) {
        String name = info.getThreadName();

        if (name.indexOf(THREAD_SUFFIX) == -1) {
            return NO_ERROR;
        }

        long id = info.getThreadId();
        Thread.State state = info.getThreadState();
        StackTraceElement[] stackTrace = info.getStackTrace();

        BaseThread thrd = findThread(id);
        if (thrd == null) {
            return ERR_THREAD_NOTFOUND;
        }

        if (!thrd.checkState(state))
            return ERR_STATE;

        if (!thrd.checkStackTrace(stackTrace))
            return ERR_STACKTRACE;

        return NO_ERROR;
    }
}

abstract class BaseThread extends Thread {

    private int currentDepth = 0;
    private String logPrefix;
    protected Log.Logger logger;

    protected ThreadController controller;

    protected List<String> expectedMethods = new ArrayList<String>();
    protected int expectedLength;

    protected ThreadsGroupLocks threadsGroupLocks;

    static {
        if (ThreadController.invocationType == ThreadController.NATIVE_TYPE ||
                ThreadController.invocationType == ThreadController.MIXED_TYPE) {
            try {
                System.loadLibrary("ThreadController");
            } catch (UnsatisfiedLinkError e) {
                System.err.println("Could not load \"ThreadController\" "
                        + "library");
                System.err.println("java.library.path:"
                        + System.getProperty("java.library.path"));
                throw e;
            }
        }
    }

    public BaseThread(ThreadController controller, String name, Log log, ThreadsGroupLocks threadsGroupLocks) {
        super(name);
        this.controller = controller;
        int pos = controller.LOG_PREFIX.indexOf('>');
        logPrefix = controller.LOG_PREFIX.substring(0, pos) + "::"
                + name + "> ";
        setLog(log);
        this.threadsGroupLocks = threadsGroupLocks;

        expectedLength = 1 + controller.maxDepth + 1;
        if(controller.invocationType == ThreadController.MIXED_TYPE) {
             //nativeRecursiveMethod
             expectedLength ++;
        }

        expectedMethods.add(BaseThread.class.getName() + ".run");

        switch (controller.invocationType) {
            case ThreadController.JAVA_TYPE:
                expectedMethods.add(BaseThread.class.getName() + ".recursiveMethod");
                break;
            case ThreadController.NATIVE_TYPE:
                expectedMethods.add(BaseThread.class.getName() + ".nativeRecursiveMethod");
                break;
            case ThreadController.MIXED_TYPE:
                expectedMethods.add(BaseThread.class.getName() + ".recursiveMethod");
                expectedMethods.add(BaseThread.class.getName() + ".nativeRecursiveMethod");
        }

        expectedMethods.add(ThreadsGroupLocks.PlainCountDownLatch.class.getName() + ".countDown");
    }

    public void run() {
        try {
            switch (controller.invocationType) {
                case ThreadController.JAVA_TYPE:
                case ThreadController.MIXED_TYPE:
                    recursiveMethod();
                    break;
                case ThreadController.NATIVE_TYPE:
                    nativeRecursiveMethod();
                    break;
                default:
                    throw new Failure("unknown invocationType:"
                            + controller.invocationType);
            }
        } catch (StackOverflowError e) {
            logger.complain(e.toString());
            throw new RuntimeException(e);
        }
        logger.trace(controller.THREAD_TRACE_LEVEL, "thread finished");
    }

    protected abstract void bringState();

    public abstract State getState();

    protected abstract void nativeBringState();

    public abstract boolean checkState(Thread.State state);

    public boolean checkStackTrace(StackTraceElement[] elements) {
        boolean res = true;

        logger.trace(controller.THREAD_TRACE_LEVEL, "trace elements: "
                + elements.length);

        if (elements.length > expectedLength) {
            res = false;
            logger.complain("Contains " + elements.length + ", more then "
                    + expectedLength + " elements");
        }

        for (int j = 0; j < elements.length; j++) {
            if (!checkElement(elements[j])) {
                logger.complain("Unexpected method name: "
                                + elements[j].getMethodName()
                                + " at " + j + " position");
                if (elements[j].isNativeMethod()) {
                    logger.complain("\tline number: (native method)");
                    logger.complain("\tclass name: " + elements[j].getClassName());
                } else {
                    logger.complain("\tline number: " + elements[j].getLineNumber());
                    logger.complain("\tclass name: " + elements[j].getClassName());
                    logger.complain("\tfile name: " + elements[j].getFileName());
                }
                res = false;
            }
        }
        return res;
    }

    protected boolean checkElement(StackTraceElement element) {
        String name = element.getClassName() + "." + element.getMethodName();
        if (expectedMethods.contains(name)) {
            return true;
        }

        logger.trace(controller.THREAD_TRACE_LEVEL, "\"" + name + "\""
                + " is not expected method name");
        return false;
    }

    protected void recursiveMethod() {
        currentDepth++;

        if (controller.maxDepth - currentDepth > 0) {

            Thread.yield();
            try {
                if (ThreadController.invocationType
                        == ThreadController.MIXED_TYPE) {
                    nativeRecursiveMethod();
                } else {
                    recursiveMethod();
                }

            } catch (StackOverflowError e) {
                logger.display(getName() + "> " + e);
            }

        } else if (controller.maxDepth == currentDepth) {
            logger.trace(controller.THREAD_TRACE_LEVEL, "state has been "
                    + "reached");
            bringState();
        }
        currentDepth--;
    }

    protected native void nativeRecursiveMethod();

    /**
     * Defines <code>Log.Logger</code> object
     */
    public void setLog(Log log) {
        logger = new Log.Logger(log, logPrefix);
    }
}

class BlockedThread extends BaseThread {

    private static final Thread.State STATE = Thread.State.BLOCKED;

    public State getState() {
        return STATE;
    }

    public BlockedThread(ThreadController controller, String name, Log log, ThreadsGroupLocks threadsGroupLocks) {
        super(controller, name, log, threadsGroupLocks);

        this.threadsGroupLocks = threadsGroupLocks;

        expectedLength += 2;

        expectedMethods.add(ThreadsGroupLocks.Blocker.class.getName() + ".block");

        switch (controller.invocationType) {
            case ThreadController.JAVA_TYPE:
                expectedMethods.add(BlockedThread.class.getName() + ".bringState");
                break;
            case ThreadController.NATIVE_TYPE:
                expectedMethods.add(BlockedThread.class.getName() + ".nativeBringState");
                break;
            case ThreadController.MIXED_TYPE:
                expectedMethods.add(BlockedThread.class.getName() + ".bringState");

        }
    }

    protected void bringState() {
        logger.trace(controller.THREAD_TRACE_LEVEL, "entering to monitor");
        threadsGroupLocks.getBarrier(getState()).countDown();
        threadsGroupLocks.blocker.block();
        logger.trace(controller.THREAD_TRACE_LEVEL, "exiting from monitor");
    }

    protected native void nativeBringState();

    public boolean checkState(Thread.State state) {
        return state == Thread.State.BLOCKED;
    }
}

class WaitingThread extends BaseThread {

    private static final Thread.State STATE = Thread.State.WAITING;
    public State getState() {
        return STATE;
    }

    private ThreadsGroupLocks threadsGroupLocks;

    public WaitingThread(ThreadController controller, String name, Log log, ThreadsGroupLocks threadsGroupLocks) {
        super(controller, name, log, threadsGroupLocks);

        this.threadsGroupLocks = threadsGroupLocks;

        expectedLength += 4;

        expectedMethods.add(ThreadsGroupLocks.PlainCountDownLatch.class.getName() + ".await");
        expectedMethods.add(Object.class.getName() + ".wait");

        switch (controller.invocationType) {
            case ThreadController.JAVA_TYPE:
                expectedMethods.add(WaitingThread.class.getName() + ".bringState");
                break;
            case ThreadController.NATIVE_TYPE:
                expectedMethods.add(WaitingThread.class.getName() + ".nativeBringState");
                break;
            case ThreadController.MIXED_TYPE:
                expectedMethods.add(WaitingThread.class.getName() + ".bringState");

        }
    }


    protected void bringState() {
        ThreadsGroupLocks.PlainCountDownLatch barrier = threadsGroupLocks.getBarrier(STATE);
        try {
            logger.trace(controller.THREAD_TRACE_LEVEL, "waiting on a monitor");
            threadsGroupLocks.getBarrier(getState()).countDown();
            barrier.await();
        } catch (InterruptedException e) {
            logger.display(e.toString());
        }
    }

    protected native void nativeBringState();

    public boolean checkState(Thread.State state) {
        return state == STATE;
    }

}

class SleepingThread extends BaseThread {
    private static final Thread.State STATE = State.TIMED_WAITING;

    public State getState() {
        return STATE;
    }

    private ThreadsGroupLocks threadsGroupLocks;

    public SleepingThread(ThreadController controller, String name, Log log, ThreadsGroupLocks threadsGroupLocks) {
        super(controller, name, log, threadsGroupLocks);

        this.threadsGroupLocks = threadsGroupLocks;

        expectedLength += 3;

        expectedMethods.add(Thread.class.getName() + ".sleep");
        expectedMethods.add(SleepingThread.class.getName() + ".run");

        switch (controller.invocationType) {
            case ThreadController.JAVA_TYPE:
                expectedMethods.add(SleepingThread.class.getName() + ".bringState");
                break;
            case ThreadController.NATIVE_TYPE:
                 expectedMethods.add(SleepingThread.class.getName() + ".nativeBringState");
                break;
            case ThreadController.MIXED_TYPE:
                 expectedMethods.add(SleepingThread.class.getName() + ".bringState");
        }

    }

    protected void bringState() {
        try {
            threadsGroupLocks.getBarrier(getState()).countDown();
            Thread.sleep(3600 * 1000);
        } catch (InterruptedException e) {
            logger.display(e.toString());
        }
    }

    protected native void nativeBringState();

    public boolean checkState(Thread.State state) {
        return state == Thread.State.TIMED_WAITING;
    }

    public void run() {
        try {
            switch (controller.invocationType) {
                case ThreadController.JAVA_TYPE:
                case ThreadController.MIXED_TYPE:
                    recursiveMethod();
                    break;
                case ThreadController.NATIVE_TYPE:
                    nativeRecursiveMethod();
                    break;
                default:
                    throw new Failure("unknown invocationType:"
                            + controller.invocationType);
            }
            logger.trace(controller.THREAD_TRACE_LEVEL, "thread finished");
        } catch (StackOverflowError e) {
            logger.complain(e.toString());
            throw new RuntimeException(e);
        }
    }
}

class RunningThread extends BaseThread {
    public State getState() {
        return STATE;
    }

    private static final Thread.State STATE = Thread.State.RUNNABLE;
    private ThreadsGroupLocks threadsGroupLocks;

    public RunningThread(ThreadController controller, String name, Log log, ThreadsGroupLocks threadsGroupLocks) {
        super(controller, name, log, threadsGroupLocks);
        this.threadsGroupLocks = threadsGroupLocks;

        expectedLength += 2;

        expectedMethods.add(Thread.class.getName() + ".yield");

        switch (controller.invocationType) {
            case ThreadController.JAVA_TYPE:
                expectedMethods.add(RunningThread.class.getName() + ".bringState");
                break;
            case ThreadController.NATIVE_TYPE:
                expectedMethods.add(RunningThread.class.getName() + ".nativeBringState");
                break;
            case ThreadController.MIXED_TYPE:
                expectedMethods.add(RunningThread.class.getName() + ".bringState");
        }
    }

    protected void bringState() {
        logger.trace(controller.THREAD_TRACE_LEVEL, "running loop");
        threadsGroupLocks.getBarrier(getState()).countDown();
        while (!threadsGroupLocks.runnableCanExit) {
            Thread.yield();
        }
    }

    protected native void nativeBringState();

    public boolean checkState(Thread.State state) {
        return state == Thread.State.RUNNABLE;
    }
}


class ThreadsGroupLocks {

    private Log.Logger logger;

    //for all
    private Map<Thread.State, PlainCountDownLatch> barriers = new HashMap<Thread.State, PlainCountDownLatch>();

    //for Blocked
    public final Blocker blocker = new Blocker();

    //for Runnable
    public volatile boolean runnableCanExit = false;

    public ThreadsGroupLocks(Map<Thread.State, Integer> threadsCount, Log.Logger logger) {
        this.logger = logger;
        for (Thread.State state : threadsCount.keySet()) {
            if (state == Thread.State.WAITING) {
                barriers.put(state, new PlainCountDownLatch(threadsCount.get(state) + 1));
            } else {
                barriers.put(state, new PlainCountDownLatch(threadsCount.get(state)));
            }
        }
        blocker.startBlocker();
    }

    public PlainCountDownLatch getBarrier(Thread.State state) {
        return barriers.get(state);
    }

    public void waitForGroup(Thread.State stateGroup) {
        switch (stateGroup) {
            case BLOCKED:
            case RUNNABLE:
            case TIMED_WAITING:
                try {
                    barriers.get(stateGroup).await();
                } catch (InterruptedException e) {
                    logger.display(e.toString());
                }
                break;

            case WAITING:
                while (barriers.get(stateGroup).getCount() != 1) {
                    Thread.yield();
                }
                break;
        }
    }

    public void releaseGroup(Thread.State stateGroup) {
        switch (stateGroup) {
            case BLOCKED:
                blocker.unBlock();
                break;
            case RUNNABLE:
                runnableCanExit = true;
                break;
            case TIMED_WAITING:
            case WAITING:
                barriers.get(stateGroup).countDown();
                break;
        }
    }

    public class Blocker {

        private Object monitor = new Object();
        private PlainCountDownLatch blockerCanExit = new PlainCountDownLatch(1);
        private PlainCountDownLatch blockerStart = new PlainCountDownLatch(1);

        private Runnable blockerThread = new Runnable() {
            public void run() {
                synchronized (monitor) {
                    blockerStart.countDown();
                    try {
                        blockerCanExit.await();
                    } catch (InterruptedException e) {
                        logger.display(e.toString());
                    }

                }
            }
        };

        public void startBlocker() {
            new Thread(blockerThread, "Blocker").start();
        }

        public void block() {
            try {
                blockerStart.await();
            } catch (InterruptedException e) {
                logger.complain(e.toString());
            }
            synchronized (monitor) {
            }
        }

        public void unBlock() {
            blockerCanExit.countDown();
        }
    }

     public static class PlainCountDownLatch {
        private volatile int counter;
        private Object counterMonitor = new Object();

        public PlainCountDownLatch(int counter){
            this.counter = counter;
        }

        public void countDown(){
            synchronized (counterMonitor) {
                counter--;
                if(counter==0) {
                    counterMonitor.notifyAll();
                }
            }
        }

        public void await() throws InterruptedException{
            synchronized (counterMonitor){
                while(counter != 0){
                    counterMonitor.wait();
                }
            }
        }

         public int getCount(){
             synchronized (counterMonitor) {
                 return counter;
             }
         }
    }

}