java创建两个线程
时间: 2023-09-24 11:11:03 浏览: 54
好的,以下是Java创建两个线程的示例代码:
```
public class TwoThreadsDemo {
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread 1 is running.");
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread 2 is running.");
}
});
thread1.start();
thread2.start();
}
}
```
在这个示例中,我们创建了两个线程`thread1`和`thread2`,它们分别输出了不同的字符串。我们通过`start()`方法启动这两个线程,让它们同时运行。注意,多线程编程需要注意同步和线程安全等问题。
相关问题
java开启两个线程
可以使用Java中的Thread类来创建和启动多个线程。以下是一个简单的示例代码,可以创建和启动两个线程:
```
public class MyThread implements Runnable {
private String name;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(name + " " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread("Thread 1"));
Thread t2 = new Thread(new MyThread("Thread 2"));
t1.start();
t2.start();
}
}
```
上述代码中,我们创建了一个名为MyThread的类,它实现了Runnable接口,并覆盖了run()方法。在run()方法中,我们使用一个简单的for循环,输出了一些信息,并使用Thread.sleep()方法使线程休眠1秒钟。
在main()方法中,我们创建了两个线程t1和t2,分别使用MyThread类的实例作为参数来初始化它们。然后,我们使用start()方法启动它们,这将调用run()方法并开始执行线程。两个线程将以并行的方式运行,并且每个线程的输出将与另一个线程的输出交织在一起。
java开启两个线程争抢
可以使用Java中的多线程机制来实现两个线程争抢。下面是一个示例代码:
```java
public class ThreadDemo implements Runnable {
private int count = 10;
public synchronized void run() {
while (count > 0) {
count--;
System.out.println(Thread.currentThread().getName() + " count = " + count);
}
}
public static void main(String[] args) {
ThreadDemo demo = new ThreadDemo();
Thread t1 = new Thread(demo, "Thread1");
Thread t2 = new Thread(demo, "Thread2");
t1.start();
t2.start();
}
}
```
在这个示例中,我们定义了一个Runnable接口的实现类ThreadDemo,其中包含了一个计数器count,并在run方法中使用synchronized关键字来保证线程安全。在main方法中,我们创建了两个线程t1和t2,并将它们都传入ThreadDemo的实例中进行执行。
当我们运行这个程序时,可以看到两个线程争夺计数器的值,输出结果如下:
```
Thread1 count = 9
Thread2 count = 8
Thread1 count = 7
Thread2 count = 6
Thread1 count = 5
Thread2 count = 4
Thread1 count = 3
Thread2 count = 2
Thread1 count = 1
```
注意,由于使用了synchronized关键字,所以每次只能有一个线程访问run方法中的代码块,保证了线程安全。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)