目录
程序、线程、进程
程序:一段静态的代码 进程:正在运行的一段程序 线程:进程可以细化为线程,是程序内部的一条执行路径
方式一:继承Thread类的方式
步骤: 创建一个继承于Thread类的子类 重写run()方法--->将这个线程要做的事情写在方法体中 创建子类的对象 通过该对象调用start()
//遍历100以内的偶数
class MyThread extends Thread {
@Override
// 另一个线程的执行
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
}
class W {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
方式二、实现Runnable接口的方式
步骤: 创建实现了Runnable接口的类 实现类去实现Runnable接口中的抽象方法:run() 创建实现类的对象 将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象 通过Thread类的对象调用start()方法
class MThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ":" + i);//这里必须要这样写
// 因为这个实现类是实现的Runnable,它里面没有getName()的方法
}
}
}
}
public class ThreadTest1 {
public static void main(String[] args) {
// 3.创建实现类的对象
MThread mThread = new MThread();
// 4.将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象
Thread t1 = new Thread(mThread);
// 再开启一个线程
Thread t2 = new Thread(mThread);
t1.setName("线程一");
t2.setName("线程二");
// 5.通过Thread类的对象调用start()方法
t1.start();
t2.start();
}
}
方式三:实现Callable接口的方式 Jdk5.0新增的
步骤: 1.创建一个实现了Callable接口的实现类 2.实现call(),把线程需要实现的操作写在call()中 3.创建实现类的对象 4.将实现类的对象作为参数传递到FutureTask类的构造器中,创建FutureTask类的对象 5.将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread类的对象并调用start(). 6.如果想要call()的返回值,就用FutureTask类的对象来调用get()来获取call()中的返回值
class NewThread implements Callable {
// 2.实现call(),把线程需要实现的操作写在call()中
public Object call() throws Exception {
int sum = 0;
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
sum += i;
}
}
return sum;
}
}
public class ThreadNew {
public static void main(String[] args) {
// 3.创建实现类的对象
NewThread n = new NewThread();
// 4.将实现类的对象作为参数传递到FutureTask类的构造器中,创建FutureTask类的对象
FutureTask f1 = new FutureTask(n);
// 5.将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread类的对象并调用start().
new Thread(f1).start();
// 6.如果想要call()的返回值,就用FutureTask类的对象来调用get()来获取call()中的返回值
try {//get()的返回值就是FutureTask的构造器参数的这个Callable的实现类里面重写的call()所返回的值
Object sum = f1.get();
System.out.println(sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
方式四:使用线程池的方式
步骤: 1.创建实现Runnable或者Callable的实现类 2.实现run()或call() 3.提供指定线程数量的线程池 如果需要对线程池进行操作时,强转为ThreadPoolExecutor类型并实例化。然后可以操作 4.执行指定线程的操作,需要提供实现了Runnable或者Callable接口的实现类的对象 5.关闭连接池
class Thread1 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
class Thread2 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 != 0) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
public class ThreadPool {
public static void main(String[] args) {
// 1.提供指定线程数量的线程池
ExecutorService e = Executors.newFixedThreadPool(10);
// 需要对线程池进行操作时,强转为ThreadPoolExecutor类型并实例化。然后可以操作
ThreadPoolExecutor e1 = (ThreadPoolExecutor) e;
e1.setCorePoolSize(19);
// 2.执行指定线程的操作,需要提供实现了Runnable或者Callable接口的实现类的对象
e.execute(new Thread1());//适用于Runnable
e.execute(new Thread2());//适用于Runnable
// e.submit();//适用于Callable
// 3.关闭连接池
e.shutdown();
}
}