重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java双线程代码,多线程java代码

java实现两个线程,一个打印奇数,一个打印偶数,这两个线程如何并发执行?

package test;

成都创新互联主营长寿网站建设的网络公司,主营网站建设方案,重庆App定制开发,长寿h5小程序制作搭建,长寿网站营销推广欢迎长寿等地区企业咨询

public class Test33{

private static int state = 1;

private static int num1 = 1;

private static int num2 = 2;

public static void main(String[] args) {

final Test33 t = new Test33();

new Thread(new Runnable() {

@Override

public void run() {

while(num1100){

//两个线程都用t对象作为锁,保证每个交替期间只有一个线程在打印

synchronized (t) {

// 如果state!=1, 说明此时尚未轮到线程1打印, 线程1将调用t的wait()方法, 直到下次被唤醒

if(state!=1){

try {

t.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

// 当state=1时, 轮到线程1打印5次数字

for(int j=0; j1; j++){

System.out.println(num1);

num1 += 2;

}

// 线程1打印完成后, 将state赋值为2, 表示接下来将轮到线程2打印

state = 2;

// notifyAll()方法唤醒在t上wait的线程2, 同时线程1将退出同步代码块, 释放t锁

t.notifyAll();

}

}

}

}).start();

new Thread(new Runnable() {

@Override

public void run() {

while(num2100){

synchronized (t) {

if(state!=2){

try {

t.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

for(int j=0; j1; j++){

System.out.println(num2);

num2 += 2;

}

state = 1;

t.notifyAll();

}

}

}

}).start();

}

}

JAVA:编写一个程序实现两个线程:

public class TestThreadOutput {

public static void main(String[] args) {

OutputInt opi=new OutputInt();

Thread th1=new Thread(opi);

Thread th2=new Thread(opi);

th1.start();

th2.start();

}

}

class OutputInt implements Runnable{

public void run() {

for(int i=10;i=15;i++){

System.out.print(i+",");

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

请采纳答案,支持我一下。

java线程的经典代码

package threadgroup;

class ThreadDemo3 extends Thread {

private String name;

private int delay;

public ThreadDemo3(String sname, int i_delay) {

name = sname;

delay = i_delay;

}

public void run() {

try {

sleep(delay);

} catch (InterruptedException e) {

}

System.out.println("多线程测试!\n" + name + "\n" + delay);

}

}

public class testMyThread {

public static void main(String[] args) {

ThreadDemo3 th1,th2,th3;

th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));

th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900));

th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));

th1.start();

th2.start();

th3.start();

}

}

package threadgroup;

public class threadDemo {

public static void main(String[] args) {

Thread t = Thread.currentThread();

t.setName("你好吗?");

System.out.println("正在进行的Thread是:" + t);

try {

for (int i = 0; i 5; i++) {

System.out.println("我不叫穆继超" + i);

Thread.sleep(3000);

}

} catch (Exception e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

}

}

package threadgroup;

public class threadDemo2 implements Runnable {

public threadDemo2() {

Thread t1 = Thread.currentThread();

t1.setName("第一个主进程");

System.out.println("正在运行" + t1);

Thread t2 = new Thread(this, "");

System.out.println("在创建一个进程");

t2.start();

try {

System.out.println("使他进入第一个睡眠状态");

Thread.sleep(2000);

} catch (InterruptedException e) {

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第一个进程");

}

public void run() {

try {

for (int i = 0; i 5; i++) {

System.out.println("进程" + i);

Thread.sleep(3000);

}

} catch (InterruptedException e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第二个进程");

}

public static void main(String[] args) {

new threadDemo2();

}

}


当前标题:java双线程代码,多线程java代码
转载注明:http://cqcxhl.com/article/hodggp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP