Example usage for java.lang Runnable Runnable

List of usage examples for java.lang Runnable Runnable

Introduction

In this page you can find the example usage for java.lang Runnable Runnable.

Prototype

Runnable

Source Link

Usage

From source file:EarlyNotify.java

public static void main(String[] args) {
    final EarlyNotify enf = new EarlyNotify();

    Runnable runA = new Runnable() {
        public void run() {
            try {
                String item = enf.removeItem();
                print("returned: '" + item + "'");
            } catch (InterruptedException ix) {
                print("interrupted!");
            } catch (Exception x) {
                print("threw an Exception!!!\n" + x);
            }//from   w  ww  . j  ava2s  .c o  m
        }
    };

    Runnable runB = new Runnable() {
        public void run() {
            enf.addItem("Hello!");
        }
    };

    try {
        Thread threadA1 = new Thread(runA, "A");
        threadA1.start();

        Thread.sleep(500);

        Thread threadA2 = new Thread(runA, "B");
        threadA2.start();

        Thread.sleep(500);

        Thread threadB = new Thread(runB, "C");
        threadB.start();

        Thread.sleep(1000);

        threadA1.interrupt();
        threadA2.interrupt();
    } catch (InterruptedException x) {
    }
}

From source file:StylesExample5.java

public static void main(String[] args) {
    try {/*w  w  w.  j  a  va  2s  .c o  m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 5");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);

    // Create and add the style
    final Style heading2Style = sc.addStyle("Heading2", null);
    heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
    heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
    heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
    heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Finally, apply the style to the heading
                    doc.setParagraphAttributes(0, 1, heading2Style, false);

                    // Set the foreground and font
                    pane.setForeground(Color.blue);
                    pane.setFont(new Font("serif", Font.PLAIN, 12));
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:Deadlock.java

public static void main(String[] args) {
    final Deadlock obj1 = new Deadlock("Thread 1");
    final Deadlock obj2 = new Deadlock("Thread 2");

    Runnable runA = new Runnable() {
        public void run() {
            obj1.checkOther(obj2);// w w  w .j  a v  a 2 s.c  o m
        }
    };

    Thread thread = new Thread(runA, "A");
    thread.start();

    try {
        Thread.sleep(200);
    } catch (InterruptedException x) {
    }

    Runnable runB = new Runnable() {
        public void run() {
            obj2.checkOther(obj1);
        }
    };

    Thread threadB = new Thread(runB, "B");
    threadB.start();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException x) {
    }

    threadPrint("finished sleeping");

    threadPrint("about to interrupt() threadA");
    thread.interrupt();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException x) {
    }

    threadPrint("about to interrupt() threadB");
    threadB.interrupt();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException x) {
    }

    threadPrint("did that break the deadlock?");
}

From source file:ChangeTrackingTest.java

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            ColorFrame frame = new ColorFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);/*  w w  w .j a va 2  s .  co m*/
        }
    });
}

From source file:ComboBoxTest.java

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {

            ComboBoxFrame frame = new ComboBoxFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);// w  w  w  .j  ava2  s  .co m
        }
    });
}

From source file:CheckBoxTest.java

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            CheckBoxFrame frame = new CheckBoxFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);//www  .  ja va  2s . com
        }
    });
}

From source file:InvestmentTable.java

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new InvestmentTableFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);/*ww w  . j av  a2  s .  c  o m*/
        }
    });
}

From source file:Main.java

public static void main(String[] args) {
    new Main().display();
    new Thread(new Runnable() {
        @Override/*from   w w w .j ava  2s  .c  o  m*/
        public void run() {
            for (int i = 0; i < 3; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println((i + 1) + "s. elapsed.");
                } catch (InterruptedException e) {
                    e.printStackTrace(System.err);
                }
            }
        }
    }).start();
}

From source file:EarlyReturn.java

public static void main(String[] args) {
    try {/*from w ww  . j  av a2s .  co  m*/
        final EarlyReturn er = new EarlyReturn(0);

        Runnable r = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(1500);
                    er.setValue(2);
                    Thread.sleep(500);
                    er.setValue(3);
                    Thread.sleep(500);
                    er.setValue(4);
                } catch (Exception x) {
                    x.printStackTrace();
                }
            }
        };

        Thread t = new Thread(r);
        t.start();

        System.out.println("waitUntilAtLeast(5, 3000)");
        long startTime = System.currentTimeMillis();
        boolean retVal = er.waitUntilAtLeast(5, 3000);
        long elapsedTime = System.currentTimeMillis() - startTime;

        System.out.println(elapsedTime + " ms, retVal=" + retVal);
    } catch (InterruptedException ix) {
        ix.printStackTrace();
    }
}

From source file:StylesExample4.java

public static void main(String[] args) {
    try {/*from   ww  w . j  a v  a 2 s  . c  o m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 4");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    JTextPane pane = new JTextPane(doc);

    // Create and add the main document style
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
    final Style mainStyle = sc.addStyle("MainStyle", defaultStyle);
    StyleConstants.setLeftIndent(mainStyle, 16);
    StyleConstants.setRightIndent(mainStyle, 16);
    StyleConstants.setFirstLineIndent(mainStyle, 16);
    StyleConstants.setFontFamily(mainStyle, "serif");
    StyleConstants.setFontSize(mainStyle, 12);

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Set the logical style
                    doc.setLogicalStyle(0, mainStyle);
                } catch (BadLocationException e) {
                }
                doc.dump(System.out);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}