Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QtThread.java
Go to the documentation of this file.
1// Copyright (C) 2018 BogDan Vatra <bogdan@kdab.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4package org.qtproject.qt.android;
5
6import java.util.ArrayList;
7import java.util.concurrent.Semaphore;
8
9public class QtThread {
10 private ArrayList<Runnable> m_pendingRunnables = new ArrayList<Runnable>();
11 private boolean m_exit = false;
12 private Thread m_qtThread = new Thread(new Runnable() {
13 @Override
14 public void run() {
15 while (!m_exit) {
16 try {
17 ArrayList<Runnable> pendingRunnables;
18 synchronized (m_qtThread) {
19 if (m_pendingRunnables.size() == 0)
20 m_qtThread.wait();
21 pendingRunnables = new ArrayList<Runnable>(m_pendingRunnables);
22 m_pendingRunnables.clear();
23 }
24 for (Runnable runnable : pendingRunnables)
25 runnable.run();
26 } catch (InterruptedException e) {
27 e.printStackTrace();
28 }
29 }
30 }
31 });
32
33 QtThread() {
34 m_qtThread.setName("qtMainLoopThread");
35 m_qtThread.start();
36 }
37
38 public void post(final Runnable runnable) {
39 synchronized (m_qtThread) {
40 m_pendingRunnables.add(runnable);
41 m_qtThread.notify();
42 }
43 }
44
45 public void run(final Runnable runnable) {
46 final Semaphore sem = new Semaphore(0);
47 synchronized (m_qtThread) {
48 m_pendingRunnables.add(new Runnable() {
49 @Override
50 public void run() {
51 runnable.run();
52 sem.release();
53 }
54 });
55 m_qtThread.notify();
56 }
57 try {
58 sem.acquire();
59 } catch (InterruptedException e) {
60 e.printStackTrace();
61 }
62 }
63
64 public void exit()
65 {
66 m_exit = true;
67 synchronized (m_qtThread) {
68 m_qtThread.notify();
69 }
70 try {
71 m_qtThread.join();
72 } catch (InterruptedException e) {
73 e.printStackTrace();
74 }
75 }
76}
void acquire(int n=1)
Tries to acquire n resources guarded by the semaphore.
void release(int n=1)
Releases n resources guarded by the semaphore.
void run(final Runnable runnable)
Definition QtThread.java:45
void post(final Runnable runnable)
Definition QtThread.java:38
double e
QSemaphore sem(5)
[0]