Java多线程实现-implement Runnable和extend Thread

Java实现线程的方法有两种
分别是:

/**
* @author LiuTaiyi
* @description implements Runnable
* @date 2019/8/22
*/	
public class testThread implements Runnable {
	@Override
	public void run() {
    	System.out.println("implements Runnable方法实现线程");
	}
}

public class testThread2 extends Thread {
	public void run() {
    	System.out.println("extends Thread 方法实现线程");
	}
}
好的,以下是基于 `Thread` 子 `Runnable` 接口创建多线程实现线程同步机制的 Java 代码示例: ### 基于 `Thread` 子创建多线程 ```java // Step 1: Create a subclass of Thread class MyThread extends Thread { private int threadNumber; public MyThread(int threadNumber) { this.threadNumber = threadNumber; } @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("Thread " + threadNumber + ": " + i); try { Thread.sleep(1000); // Simulate work being done } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ThreadExample { public static void main(String[] args) { MyThread thread1 = new MyThread(1); MyThread thread2 = new MyThread(2); thread1.start(); thread2.start(); } } ``` ### 基于 `Runnable` 接口创建多线程 ```java // Step 1: Implement the Runnable interface class MyRunnable implements Runnable { private int threadNumber; public MyRunnable(int threadNumber) { this.threadNumber = threadNumber; } @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("Thread " + threadNumber + ": " + i); try { Thread.sleep(1000); // Simulate work being done } catch (InterruptedException e) { e.printStackTrace(); } } } } public class RunnableExample { public static void main(String[] args) { MyRunnable runnable1 = new MyRunnable(1); MyRunnable runnable2 = new MyRunnable(2); Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2); thread1.start(); thread2.start(); } } ``` ### 实现线程同步机制 #### 基于 `Thread` 子的同步 ```java class Counter { private int count = 0; public synchronized void increment() { count++; System.out.println("Count: " + count); } } class SynchronizedThread extends Thread { private Counter counter; public SynchronizedThread(Counter counter) { this.counter = counter; } @Override public void run() { for (int i = 0; i < 5; i++) { counter.increment(); try { Thread.sleep(1000); // Simulate work being done } catch (InterruptedException e) { e.printStackTrace(); } } } } public class SynchronizedThreadExample { public static void main(String[] args) { Counter counter = new Counter(); SynchronizedThread thread1 = new SynchronizedThread(counter); SynchronizedThread thread2 = new SynchronizedThread(counter); thread1.start(); thread2.start(); } } ``` #### 基于 `Runnable` 接口的同步 ```java class SynchronizedRunnable implements Runnable { private Counter counter; public SynchronizedRunnable(Counter counter) { this.counter = counter; } @Override public void run() { for (int i = 0; i < 5; i++) { counter.increment(); try { Thread.sleep(1000); // Simulate work being done } catch (InterruptedException e) { e.printStackTrace(); } } } } public class SynchronizedRunnableExample { public static void main(String[] args) { Counter counter = new Counter(); SynchronizedRunnable runnable1 = new SynchronizedRunnable(counter); SynchronizedRunnable runnable2 = new SynchronizedRunnable(counter); Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2); thread1.start(); thread2.start(); } } ``` 这些代码示例展示了如何使用 `Thread` 子 `Runnable` 接口创建多线程,并实现线程同步机制以确保多个线程安全地访问共享资源。希望这些示例对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值