Site Search:

SingleThreadRender.java

The following program simulate a web-browser that renders a webpage with text and images. Most of the time is spending on downloading images (simulated with Thread.sleep(2000)). As a single threaded program, it takes about 11 seconds to finish rendering the page. The user experience will be terrible, the user have to glare at the half rendered page for 14 seconds before he/she can make sense of the page.


Concurrency>cat SingleThreadRender.java 
import java.util.*;

/**
 * SingleThreadRendere
 * <p/>
 * Rendering page elements sequentially
 */
public abstract class SingleThreadRender {
    public static void main(String[] args) {
        SingleThreadRender str = new SingleThreadRender() {

            @Override
            void renderText(CharSequence s) {
                try {
                    Thread.sleep(2000);
                    System.out.println(Thread.currentThread().toString() + " renders text finished.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("rendering text: " + s);
                
            }

            @Override
            List<ImageInfo> scanForImageInfo(CharSequence s) {
                List<ImageInfo> imageInfos = new ArrayList<>();
                for(int i = 0; i < 5; i++) {
                    imageInfos.add(new ImageInfo() {
                        @Override
                        public ImageData downloadImage() {
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            return new ImageData() {};
                        }
                        
                    });
                }
                return imageInfos;
            }

            @Override
            void renderImage(ImageData i) {
                System.out.println("rendering image: " + i.toString());
            }
        };
        
        str.renderPage("page HTML code here");
    }
    
    void renderPage(CharSequence source) {
        renderText(source);
        List<ImageData> imageData = new ArrayList<ImageData>();
        for (ImageInfo imageInfo : scanForImageInfo(source))
            imageData.add(imageInfo.downloadImage());
        for (ImageData data : imageData)
            renderImage(data);
        renderText(source);
    }

    interface ImageData {
    }

    interface ImageInfo {
        ImageData downloadImage();
    }

    abstract void renderText(CharSequence s);
    abstract List<ImageInfo> scanForImageInfo(CharSequence s);
    abstract void renderImage(ImageData i);
}
Concurrency>javac SingleThreadRender.java 
Concurrency>date; java SingleThreadRender; date
Sat Jul 15 16:47:06 EDT 2017
Thread[main,5,main] renders text finished.
rendering text: page HTML code here
rendering image: SingleThreadRender$1$1$1@70dea4e
rendering image: SingleThreadRender$1$1$1@5c647e05
rendering image: SingleThreadRender$1$1$1@33909752
rendering image: SingleThreadRender$1$1$1@55f96302
rendering image: SingleThreadRender$1$1$1@3d4eac69
Thread[main,5,main] renders text finished.
rendering text: page HTML code here
Sat Jul 15 16:47:20 EDT 2017

Concurrency>