Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:为什么/什么是这些线程监视?_Java_Multithreading - Fatal编程技术网

Java:为什么/什么是这些线程监视?

Java:为什么/什么是这些线程监视?,java,multithreading,Java,Multithreading,我有一个多线程Java应用程序,它将图像分割成4个块,然后4个线程(我有一个四核CPU)分别处理图像的单个块,将其转换为灰度 由于某种原因,我发现它非常慢,所以我使用了NetBeans探查器,发现线程在“监视”(等待)很多。比如说, (绿色=运行,红色=监控) 我对不同数量的线程进行了实验,例如2个线程,发现这种情况仍然存在(只有1个线程没有出现) 在线程内部,我注释掉了部分代码,直到我将“大延迟”缩小到以下语句: newImage.setRGB(i,j,newColor.getRGB());

我有一个多线程Java应用程序,它将图像分割成4个块,然后4个线程(我有一个四核CPU)分别处理图像的单个块,将其转换为灰度

由于某种原因,我发现它非常慢,所以我使用了NetBeans探查器,发现线程在“监视”(等待)很多。比如说,

(绿色=运行,红色=监控)

我对不同数量的线程进行了实验,例如2个线程,发现这种情况仍然存在(只有1个线程没有出现)

在线程内部,我注释掉了部分代码,直到我将“大延迟”缩小到以下语句:

newImage.setRGB(i,j,newColor.getRGB()); // Write the new value for that pixel
如果将其注释掉,代码运行速度会快很多(几乎是5倍),并且没有线程监控:

那么为什么这一行会造成如此多的延迟呢?是不是颜色库(与BuffereImage一起)?现在,我将尝试获取一个int数组作为RGB值,而不是使用颜色对象,然后看看结果如何

以下是源代码:

PixelsManipulation.java(主类):

公共最终类像素操作{
私有静态顺序Grayscaler=新顺序();
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException、InterruptedException{
File File=新文件(“src/pixelsmanipulation/hiresimage.jpg”);
FileInputStream fis=新的FileInputStream(文件);
buffereImage image=ImageIO.read(fis);//读取图像文件
int rows=2;//2行和2列将图像分割为四分之一
int cols=2;
int chunks=rows*cols;//4个块,图像的每四分之一个块
int chunkWidth=image.getWidth()/cols;//确定块的宽度和高度
int chunkHeight=image.getHeight()/行;
整数计数=0;
BuffereImage imgs[]=新BuffereImage[chunks];//用于保存图像块的数组
对于(intx=0;x
Parallel.java:

// Let each of the 4 threads work on a different quarter of the image
public class Parallel extends Thread{//implements Runnable{

private String threadName;
private static BufferedImage myImage; // Calling it "my" image because each thread will have its own unique quarter of the image to work on
private static int width, height; // Image params

Parallel(String name, BufferedImage image){
    threadName = name;
    System.out.println("Creating "+ threadName);
    myImage = image;
    width = myImage.getWidth();
    height = myImage.getHeight();

}

public void run(){
    System.out.println("Running " + threadName);

    // Pixel by pixel (for our quarter of the image)
    for (int j = 0; j < height; j++){
        for (int i = 0; i < width; i++){

            // Traversing the image and converting the RGB values (doing the same thing as the sequential code but on a smaller scale)
            Color c = new Color(myImage.getRGB(i,j));

            int red = (int)(c.getRed() * 0.299);
            int green = (int)(c.getGreen() * 0.587);
            int blue  = (int)(c.getBlue() * 0.114);

            Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue);

            myImage.setRGB(i,j,newColor.getRGB()); // Write the new value for that pixel


        }
    }

    File output = new File("src/pixelsmanipulation/"+threadName+"grayscale.jpg"); // Put it in a "lower level" folder so we can see it in the project view
    try {
        ImageIO.write(newImage, "jpg", output);
    } catch (IOException ex) {
        Logger.getLogger(Parallel.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Thread " + threadName + " exiting. ---");
}
}
//让4个线程中的每一个在图像的不同四分之一上工作
公共类并行扩展线程{//实现可运行{
私有字符串threadName;
private static BufferedImage myImage;//将其称为“我的”映像,因为每个线程都有自己独特的映像四分之一要处理
私有静态整型宽度,高度;//图像参数
并行(字符串名称、缓冲区图像){
threadName=名称;
System.out.println(“创建”+threadName);
我的图像=图像;
width=myImage.getWidth();
height=myImage.getHeight();
}
公开募捐{
System.out.println(“Running”+threadName);
//逐像素(针对图像的四分之一)
对于(int j=0;j
我是Java线程(以及使用BuffereImage)的初学者,只是好奇
// Let each of the 4 threads work on a different quarter of the image
public class Parallel extends Thread{//implements Runnable{

private String threadName;
private static BufferedImage myImage; // Calling it "my" image because each thread will have its own unique quarter of the image to work on
private static int width, height; // Image params

Parallel(String name, BufferedImage image){
    threadName = name;
    System.out.println("Creating "+ threadName);
    myImage = image;
    width = myImage.getWidth();
    height = myImage.getHeight();

}

public void run(){
    System.out.println("Running " + threadName);

    // Pixel by pixel (for our quarter of the image)
    for (int j = 0; j < height; j++){
        for (int i = 0; i < width; i++){

            // Traversing the image and converting the RGB values (doing the same thing as the sequential code but on a smaller scale)
            Color c = new Color(myImage.getRGB(i,j));

            int red = (int)(c.getRed() * 0.299);
            int green = (int)(c.getGreen() * 0.587);
            int blue  = (int)(c.getBlue() * 0.114);

            Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue);

            myImage.setRGB(i,j,newColor.getRGB()); // Write the new value for that pixel


        }
    }

    File output = new File("src/pixelsmanipulation/"+threadName+"grayscale.jpg"); // Put it in a "lower level" folder so we can see it in the project view
    try {
        ImageIO.write(newImage, "jpg", output);
    } catch (IOException ex) {
        Logger.getLogger(Parallel.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Thread " + threadName + " exiting. ---");
}
}