这篇文章主要是个人的学习笔记,是以例子来驱动的,加深自己对多线程的理解. 一:实现多线程的两种方法 1.继承Thread class MyThread1 extends Thread{ public void run(){ System.out.println("这是自定义的线程"); } } 通过继承Thread,然后重写Thread中的run方法,把自己要实现的方法写在run()中 class ThreadTest1{ public static void main(String[…
IO密集型是指对IO操作较多的任务.下面以查询一些股票价格任务为例: YahooFinance.java public class YahooFinance { public static double getPrice(final String ticker) throws IOException{ final URL url = new URL("http://ichart.finance.yahoo.com/table.csv?s=" + ticker); final Buffe…
currentThread():返回代码正在被哪个线程调用. public class CurrentThreadWay { public static void main(String[] args) { ThreadTest t = new ThreadTest(); t.start(); } } public class ThreadTest extends Thread{ public ThreadTest(){ System.out.println("调用构造方法的线程是:"…
首先是一段代码: public class HasSelfPrivateNum { public void addI(String username){ try { int num=0; if(username.equals("a")){ num=100; System.out.println("a set over! "); Thread.sleep(2000); }else{ num=200; System.out.println("b set ove…
currentThread():返回代码正在被哪个线程调用. public class CurrentThreadWay { public static void main(String[] args) { ThreadTest t = new ThreadTest(); t.start(); } } public class ThreadTest extends Thread{ public ThreadTest(){ System.out.println("调用构造方法的线程是:"…