Java中线程组(ThreadGroup类)

线程组表示一个线程的集合。线程组也可以包含其他线程组。线程执行完就会被回收。

private static void methodA() {
    MyThread my = new MyThread() ;
        
    //创建线程类对象
        
    Thread t1 = new Thread(my, "线程1") ;
    Thread t2 = new Thread(my, "线程2") ;
        
    //public final ThreadGroup getThreadGroup()返回该线程所属的线程组
    ThreadGroup tg1 = t1.getThreadGroup() ;
    ThreadGroup tg2 = t2.getThreadGroup() ;
        
    //public final String getName():返回线程组的名称
    System.out.println(tg1.getName()); //main
    System.out.println(tg2.getName());//main
        
    //所有的线程它默认的线程组名称:main(主线程),主线程是包含在线程组里面的
    System.out.println(Thread.currentThread().getThreadGroup().getName());//main
}
private static void methodB() {
       //public ThreadGroup(String name)构造一个新线程组
    ThreadGroup tg = new ThreadGroup("新的线程组") ;
        
    MyThread my = new MyThread() ;
    
        //Thread(ThreadGroup group, Runnable target, String name)
    Thread t1 = new Thread(tg, my, "线程1") ;
    Thread t2 = new Thread(tg, my, "线程2") ;
        
    //直接获取线程组名称
    System.out.println(t1.getThreadGroup().getName());
    System.out.println(t2.getThreadGroup().getName());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java OpenCV中绘制两个点之间的中线,可以按照以下步骤进行操作: 1. 首先,使用OpenCV的 `line()` 函数绘制两个点之间的直线。这个函数需要传入三个参数:图像、起点坐标和终点坐标。 2. 然后,计算出这两个点的中心坐标。可以使用以下代码计算中心坐标: ``` int center_x = (start_x + end_x) / 2; int center_y = (start_y + end_y) / 2; ``` 3. 最后,使用 `line()` 函数再次将图像中心与终点坐标之间的直线绘制出来。这个函数需要传入三个参数:图像、中心坐标和终点坐标。 完整的代码示例: ``` import org.opencv.core.Core; import org.opencv.core.Point; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class DrawLine { public static void main(String[] args) { // Load the OpenCV library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Load the image String filename = "image.jpg"; Mat img = Imgcodecs.imread(filename); // Define the start and end points Point start = new Point(100, 100); Point end = new Point(200, 200); // Draw the line between the two points Imgproc.line(img, start, end, new Scalar(0, 255, 0), 2); // Calculate the center point int center_x = (int)((start.x + end.x) / 2); int center_y = (int)((start.y + end.y) / 2); Point center = new Point(center_x, center_y); // Draw the line between the center point and the end point Imgproc.line(img, center, end, new Scalar(0, 0, 255), 2); // Display the image HighGui.imshow("Image", img); HighGui.waitKey(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值