1.继承Thread类,重写该类的run()方法。
class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
for (i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 30) {
Thread myThread1 = new MyThread();
Thread myThread2 = new MyThread();
myThread1.start();
myThread2.start();
}
}
}
}
2.实现Runnable接口,并重写该接口的run()方法
class MyRunnable implements Runnable {
private int i = 0;
@Override
public void run() {
for (i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 30) {
Runnable myRunnable = new MyRunnable();
Thread thread1 = new Thread(myRunnable);
Thread thread2 = new Thread(myRunnable);
thread1.start();
thread2.start();
}
}
}
}
3.比较简洁的写法
new Thread(new Runnable() {
@Override
public void run() {
//具体实现
}
}).start();
这种写法是利用了匿名内部类
同等这种写法,实例化了一个Runnable接口子类的实例
Thread t=new Thread(new MyRunnable());
public class MyRunnable implements Runnable{
@Override
public void run() {
//具体实现
}
}
t.start();
4.使用Callable和Future接口创建线程
public class ThreadTest {
public static void main(String[] args) {
Callable<Integer> myCallable = new MyCallable();
FutureTask<Integer> ft = new FutureTask<Integer>(myCallable);
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 30) {
Thread thread = new Thread(ft);
thread.start();
}
}
System.out.println("主线程for循环执行完毕..");
try {
int sum = ft.get();
System.out.println("sum = " + sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class MyCallable implements Callable<Integer> {
private int i = 0;
// 与run()方法不同的是,call()方法具有返回值
@Override
public Integer call() {
int sum = 0;
for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
sum += i;
}
return sum;
}
}
以下是生产者消费者模式
public class ProducerConsumer {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer pp = new Producer(ss);
Consumer cc = new Consumer(ss);
new Thread(pp).start();
new Thread(cc).start();
}
}
class WoTou {
int id;
WoTou(int id) {
this.id = id;
}
public String toString() {
return "WoTou id:" + id;
}
}
class SyncStack {
int index = 0;
WoTou[] arrWt = new WoTou[6];
public synchronized void push(WoTou wt) {
while (index == arrWt.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 叫醒一个现在正在wait该对象上的线程
this.notify();
arrWt[index] = wt;
index++;
}
public synchronized WoTou pop() {
while (index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
index--;
return arrWt[index];
}
}
// 生产
class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for (int i = 0; i < 6; i++) {
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
try {
Thread.sleep((int) Math.random() * 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 消费
class Consumer implements Runnable {
SyncStack ss = null;
Consumer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for (int i = 0; i < 6; i++) {
WoTou wt = ss.pop();
System.out.println("消费了:" + wt);
try {
Thread.sleep((int) Math.random() * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
sleep()使线程睡过去,也就是暂停执行,或者让出cpu给别的线程使用..
wait()是线程等待事件的发生,如果你的线程需要某个条件才能继续运行,就用wait(),他不占资源,只是不运行线程而已 等待命令的意思
notify()通知相应的在等待的线程条件已经满足,可以启动了,采用wait()的线程只能够通过notify或者是notifyAll()启动,不能够自己启动的.
// sleep 时别的线程不可以访问锁定对象
相关推荐
Java提供了一系列的并发工具和框架,如线程池、锁、并发容器(如ConcurrentHashMap)等,用于优化多线程编程。理解并发原理,熟练运用这些工具,能够有效提高系统的响应速度和吞吐量。 其次,分布式系统是大型...
包含计算机网络、多线程、数据库、分布式等专题。 微服务架构面试专题系列 几本霸占我休息时间的PDF 分享网约车 分享redis 大厂面试 Java面试题库 Java面试大全+spring Java精品进阶书籍 多线程 MySQL...
本文档是Java入门基础的学习资源,涵盖Java开发入门、Java编程基础、面向对象、多线程、集合框架、IO流、网络编程、安全加密、反射机制、新特性和内存管理等多方面的内容。 Java开发入门 * JDK、JRE、JVM的区别与...
同步机制是一种用于协调多个线程之间访问共享数据的方法,它可以避免资源冲突和数据竞争,确保线程之间的正确交互。 垃圾回收器 垃圾回收器是 Java 虚拟机的一种组成部分,它负责自动回收不再使用的对象,并释放...
项目介绍常见问题(必看)面试指北(定位教程)纪实面试首席执行官面试求职指南用户体验制作指南常见面试题自测(付费)面试词汇扫盲优质面经汇总(付费)项目经验指南常见面试题目JavaJava基础Java集合Java多线程...
5. **创建Session**:从SessionFactory获取Session对象,它是与数据库的单线程会话,负责事务管理和数据库操作。 6. **事务管理**:使用Session的`beginTransaction()`和`commit()`方法进行事务控制。 7. **CRUD操作...
2. **内存池管理子系统**:管理共享缓冲区,确保多进程或多线程高效访问数据库,同时支持自定义的文件和页内存分配。 3. **事务子系统**:提供事务管理功能,遵循ACID原则,保证数据一致性。通过两段锁和先写日志...
6. **多线程**:Java内置的Thread类和Runnable接口,线程同步机制(如synchronized关键字、wait()、notify()和notifyAll()方法)。 7. **网络编程**:Socket通信,ServerSocket和Socket类的应用。 8. **反射API**...