一个线程控制另一个线程的暂停或启动

MainTest类中可以控制线程的暂停或继续运行。

public class MainTest {

	/**
	 * 这个线程操作另一个线程的暂停或开始
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread1 th1 = new Thread1();
		Thread t1 = new Thread(th1);
		t1.start();
		try {
			Thread.sleep(20);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//thread线程暂停
		th1.wait1();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//thread线程继续运行
		th1.start1();
		//th1.wait1();
		//th1.start1();
	}

}

下面是thread类

public class Thread1 implements Runnable {
	private String flag = "start";
	private String control = "";

	public void run() {
		// TODO Auto-generated method stub
		int i = 0;
		while (true) {
			if (flag.equals("start")) {
				i++;
				System.out.println("The thread1 is running" + i);
			} else if (flag.equals("wait")) {
				try {
					System.out.println("===wait===");
					synchronized (control) {
						control.wait();
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public void wait1() {
		this.flag = "wait";
	}

	public void start1() {
		this.flag = "start";
		if (flag.equals("start")) {
			synchronized (control) {
				control.notifyAll();
			}
		}
	}

}
定义了一个标志位。还利用了synchronized同步关键字

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 PyQt 中,可以通过将按键的 clicked 信号连接到一个槽函数中,来控制视频线程启动暂停。以下是一个简单的例子: ```python import sys import threading import cv2 from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton from PyQt5.QtCore import Qt, QThread, pyqtSignal class VideoThread(QThread): change_pixmap_signal = pyqtSignal(QImage) def __init__(self, video_path): super().__init__() self.video_path = video_path self.is_running = False def run(self): self.is_running = True cap = cv2.VideoCapture(self.video_path) while self.is_running: ret, frame = cap.read() if ret: rgb_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = rgb_img.shape bytes_per_line = ch * w qt_img = QImage(rgb_img.data, w, h, bytes_per_line, QImage.Format_RGB888) self.change_pixmap_signal.emit(qt_img) else: break cap.release() def stop(self): self.is_running = False self.wait() class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.lbl = QLabel(self) self.lbl.resize(640, 480) self.btn = QPushButton('启动', self) self.btn.move(20, 520) self.btn.clicked.connect(self.on_btn_clicked) self.video_thread = VideoThread('video.mp4') self.video_thread.change_pixmap_signal.connect(self.update_image) self.setGeometry(300, 300, 680, 560) self.setWindowTitle('视频播放器') self.show() def on_btn_clicked(self): if self.btn.text() == '启动': self.btn.setText('暂停') self.video_thread.start() else: self.btn.setText('启动') self.video_thread.stop() def update_image(self, img): self.lbl.setPixmap(QPixmap.fromImage(img)) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 在上面的例子中,创建了一个 VideoThread 来处理视频播放,并将其 change_pixmap_signal 信号连接到槽函数 update_image 中,用于更新视频帧。同,通过在 Example 中创建一个 QPushButton 控件,并将其 clicked 信号连接到槽函数 on_btn_clicked 中,来控制视频线程启动暂停。在槽函数中,根据 QPushButton 控件的文本来判断是启动还是暂停视频线程,并分别执行相应的操作。可以根据需求修改相应的视频路径和控件的文本和行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值