List of usage examples for java.lang Runnable Runnable
Runnable
From source file:Main.java
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override//from ww w . j a v a2 s. c o m public void run() { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JScrollPane(new MyTextArea())); frame.pack(); frame.setVisible(true); } }); }
From source file:Test.java
public static void main(String[] args) throws InterruptedException { BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); for (int i = 0; i < 10; i++) { final int localI = i; queue.add(new Runnable() { public void run() { doExpensiveOperation(localI); }//w ww . ja va 2s. c om }); } ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 1000, TimeUnit.MILLISECONDS, queue); executor.prestartAllCoreThreads(); executor.shutdown(); executor.awaitTermination(100000, TimeUnit.SECONDS); }
From source file:Main.java
public static void main(String args[]) { executor = Executors.newScheduledThreadPool(1); runnable = new Runnable() { @Override//from w w w . j a v a 2 s .com public void run() { System.out.println("Inside runnable" + i++); } }; future = executor.scheduleWithFixedDelay(runnable, 0, 2, TimeUnit.SECONDS); try { Thread.sleep(4000); // sleeping for 2 seconds } catch (InterruptedException e) { e.printStackTrace(); } future.cancel(false); future = executor.scheduleWithFixedDelay(runnable, 0, 10, TimeUnit.SECONDS); }
From source file:Main.java
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try { frame.add(new MyPanel()); } catch (Exception e) { e.printStackTrace();//from w ww. j av a2 s . c o m } frame.setSize(250, 250); frame.setVisible(true); } }); }
From source file:PropertyChangeListenerDemo.java
public static void main(String args[]) { Runnable runner = new Runnable() { public void run() { JFrame frame = new JFrame("Button Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button1 = new JButton("Select Me"); final JButton button2 = new JButton("No Select Me"); final Random random = new Random(); // Define ActionListener ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JButton button = (JButton) actionEvent.getSource(); int red = random.nextInt(255); int green = random.nextInt(255); int blue = random.nextInt(255); button.setBackground(new Color(red, green, blue)); }/*from w w w.j a va2s . c om*/ }; // Define PropertyChangeListener PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent propertyChangeEvent) { String property = propertyChangeEvent.getPropertyName(); if ("background".equals(property)) { button2.setBackground((Color) propertyChangeEvent.getNewValue()); } } }; button1.addActionListener(actionListener); button1.addPropertyChangeListener(propertyChangeListener); button2.addActionListener(actionListener); frame.add(button1, BorderLayout.NORTH); frame.add(button2, BorderLayout.SOUTH); frame.setSize(300, 100); frame.setVisible(true); } }; EventQueue.invokeLater(runner); }
From source file:Main.java
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override//www .jav a 2s. co m public void run() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new BackgroundPane()); frame.pack(); frame.setVisible(true); } }); }
From source file:Main.java
public static void main(String[] args) { Runnable r = new Runnable() { @Override// w w w .j a v a2 s . co m public void run() { JPanel gui = new JPanel(); final AnimatedImage[] tiles = new AnimatedImage[2]; for (int ii = 0; ii < tiles.length; ii++) { tiles[ii] = new AnimatedImage(); gui.add(new JLabel(new ImageIcon(tiles[ii]))); } ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < tiles.length; i++) { tiles[i].paintImage(); gui.repaint(); } } }; Timer timer = new Timer(50, listener); timer.start(); JOptionPane.showMessageDialog(null, gui); timer.stop(); } }; SwingUtilities.invokeLater(r); }
From source file:Test.java
public static void main(String[] args) { final AtomicLong orderIdGenerator = new AtomicLong(0); final List<Item> orders = Collections.synchronizedList(new ArrayList<Item>()); for (int i = 0; i < 10; i++) { Thread orderCreationThread = new Thread(new Runnable() { public void run() { for (int i = 0; i < 10; i++) { long orderId = orderIdGenerator.incrementAndGet(); Item order = new Item(Thread.currentThread().getName(), orderId); orders.add(order);/*from w ww.ja va 2 s . co m*/ } } }); orderCreationThread.setName("Order Creation Thread " + i); orderCreationThread.start(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Set<Long> orderIds = new HashSet<Long>(); for (Item order : orders) { orderIds.add(order.getID()); System.out.println("Order id:" + order.getID()); } }
From source file:Main.java
public static void main(String[] args) throws Exception { URL url = new URL("http://www.java2s.com/style/download.png"); final BufferedImage bi = ImageIO.read(url); final String size = bi.getWidth() + "x" + bi.getHeight(); SwingUtilities.invokeLater(new Runnable() { public void run() { JLabel l = new JLabel(size, new ImageIcon(bi), SwingConstants.RIGHT); JOptionPane.showMessageDialog(null, l); }/*from w w w .ja v a2 s. c om*/ }); }
From source file:Main.java
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override//from w ww.j a va2 s . co m public void run() { new TimeZoneSpinners().makeUI(); } }); }