标签:imp 输入 线程 while except ++ print 数据 public
Thread t1 = new Thread(); t1.start();
Thread t2 = new Thread(); t1.run();
Thread t1 = new Thread() { @Override public void run() { System.out.println("Hello,I am 他"); } }; t1.start(); //Java8 版本 Thread t2 = new Thread(() -> System.out.println("Hello,I am 他")); t2.start(); public class CreateThread implements Runnable { public static void main(String[] args) { Thread t1 = new Thread(new CreateThread()); t1.start(); } /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object‘s * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see Thread#run() */ @Override public void run() { System.out.println("Oh, I am Runnable"); } }
public class CreateThread implements Runnable { volatile boolean stopme = false; public void stopMe() { stopme = true; } @Override public void run() { while (true) { if (stopme) { System.out.println("exit by stop me"); break; } synchronized (u) { int v = (int) (System.currentTimeMillis() / 1000); u.setId(v); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } u.setName(String.valueOf(v)); } Thread.yield(); } } }
Thread t1 = new Thread(new CreateThread()); t1.interrupt();//中断线程 实例方法 t1.isInterrupted();//判断是否中断 实例方法 Thread.interrupted();//判断是否中断,并清除当前中断状态 静态方法 public class CreateThread implements Runnable { @Override public void run() { while (true) { if (Thread.currentThread().isInterrupted()) { System.out.println("Interrupted"); break; } try { Thread.sleep(2000);//实例线程 休眠2s 当线程休眠中被中断 抛出InterruptedException 异常 } catch (InterruptedException e) { System.out.println("Interrupted When Sleep"); Thread.currentThread().interrupt(); } Thread.yield(); } } public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new CreateThread()); t1.start(); Thread.sleep(1000);//main 休眠1s 后执行中断 t1.interrupt(); } }
public class SimpleWN { final static Object object = new Object(); public static class T1 extends Thread { public void run() { synchronized (object) { System.out.println(System.currentTimeMillis() + ":T1 start!"); try { System.out.println(System.currentTimeMillis() + ":T1 wait for object "); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(System.currentTimeMillis() + ":T1 end! "); } } } public static class T2 extends Thread { public void run() { synchronized (object) { System.out.println(System.currentTimeMillis() + ":T2 start! notify one thread "); object.notify(); System.out.println(System.currentTimeMillis() + ":T2 end! "); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { Thread t1 = new T1(); Thread t2 = new T2(); t1.start(); t2.start(); } }
public final synchronized void join(long millis)throws InterruptedException public final void join() throws InterruptedException
public class JoinMain { public volatile static int i = 0; public static class AddThread extends Thread { public void run() { for (i = 0; i < 10000000; i++) ; } } public static void main(String[] args) throws InterruptedException { AddThread at = new AddThread(); at.start(); at.join(); System.out.println(i); } }
标签:imp 输入 线程 while except ++ print 数据 public
原文地址:http://www.cnblogs.com/ten951/p/6171006.html