求java多线程编程代码,要有注释,非诚勿扰~~~做出后分不是问题~~
求一个运用java多线程编程的代码,要有注释,复制粘贴的算了,谢谢!!对了,忘了说,结果要有图形界面的,麻烦了各位~~...
求一个运用java多线程编程的代码,要有注释,复制粘贴的算了,谢谢!!
对了,忘了说,结果要有图形界面的,麻烦了各位~~ 展开
对了,忘了说,结果要有图形界面的,麻烦了各位~~ 展开
展开全部
这个是实验时候做的例子 希望对你有用 包括实现多线程的两种方法
一、用创建Thread 类的子类的方法实现多线程一
源程序:
class FruitThread extends Thread{
public FruitThread(String str){
super(str);
}
public void run(){ //线程的run方法
for(int i=0;i<5;i++){
System.out.println(i+" "+getName());
try{
sleep((int)(Math.random()*1000));//等待
}
catch(InterruptedException e){
}
}
}
}
public class TwoFtuit{
public static void main(String[] a){
new FruitThread("苹果").start(); //第一个线程
new FruitThread("梨").start(); //第二个线程
}
}
实验结果:
E:\>javac TwoFtuit.java
E:\>java TwoFtuit
0 苹果
0 梨
1 苹果
2 苹果
1 梨
3 苹果
2 梨
4 苹果
3 梨
4 梨
二、用实现Runnable 接口的方法实现多线程
源程序:
class outputClass implements Runnable{
String name;
outputClass(String s){
name =s;
}
public void run(){
for(int i=0;i<3;i++){
System.out.println(name);
Thread.yield();
}
}
}
public class runThreads{
public static void main(String[] a){
outputClass out1=new outputClass("Thread1");
outputClass out2=new outputClass("Thread2");
Thread t1=new Thread(out1);
Thread t2=new Thread(out2);
t1.start();
t2.start();
}
}
实验结果:
E:\java>javac runThreads.java
E:\java>java runThreads
Thread1
Thread2
Thread1
Thread2
Thread1
Thread2
一、用创建Thread 类的子类的方法实现多线程一
源程序:
class FruitThread extends Thread{
public FruitThread(String str){
super(str);
}
public void run(){ //线程的run方法
for(int i=0;i<5;i++){
System.out.println(i+" "+getName());
try{
sleep((int)(Math.random()*1000));//等待
}
catch(InterruptedException e){
}
}
}
}
public class TwoFtuit{
public static void main(String[] a){
new FruitThread("苹果").start(); //第一个线程
new FruitThread("梨").start(); //第二个线程
}
}
实验结果:
E:\>javac TwoFtuit.java
E:\>java TwoFtuit
0 苹果
0 梨
1 苹果
2 苹果
1 梨
3 苹果
2 梨
4 苹果
3 梨
4 梨
二、用实现Runnable 接口的方法实现多线程
源程序:
class outputClass implements Runnable{
String name;
outputClass(String s){
name =s;
}
public void run(){
for(int i=0;i<3;i++){
System.out.println(name);
Thread.yield();
}
}
}
public class runThreads{
public static void main(String[] a){
outputClass out1=new outputClass("Thread1");
outputClass out2=new outputClass("Thread2");
Thread t1=new Thread(out1);
Thread t2=new Thread(out2);
t1.start();
t2.start();
}
}
实验结果:
E:\java>javac runThreads.java
E:\java>java runThreads
Thread1
Thread2
Thread1
Thread2
Thread1
Thread2
展开全部
class ThreadA extends Thread{
private Test t;
public ThreadA(Test t){
this.t=t;
}
public void run(){
for(;;){
System.out.println("线程A:"+t.age++);
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ThreadB extends Thread{
private Test t;
public ThreadB(Test t){
this.t=t;
}
public void run(){
for(;;){
System.out.println("线程B:"+t.age++);
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Test{
public int age=0;
public static void main(String args[]){
Test t=new Test();
Thread a=new ThreadA(t);
a.start();
Thread b=new ThreadB(t);
b.start();
}
}
private Test t;
public ThreadA(Test t){
this.t=t;
}
public void run(){
for(;;){
System.out.println("线程A:"+t.age++);
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ThreadB extends Thread{
private Test t;
public ThreadB(Test t){
this.t=t;
}
public void run(){
for(;;){
System.out.println("线程B:"+t.age++);
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Test{
public int age=0;
public static void main(String args[]){
Test t=new Test();
Thread a=new ThreadA(t);
a.start();
Thread b=new ThreadB(t);
b.start();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package javathread;
public class TestRunnable {
public static void main(String[] args) {
CangKu ck = new CangKu();
Procedus p1 = new Procedus(ck);
Comer c = new Comer(ck);
new Thread(p1).start();
new Thread(c).start();
}
}
class ManTou{
private int i;
public ManTou(int i) {
this.i = i;
}
public String toString(){
return "馒头--" + i;
}
}
class CangKu{
int index = 0;
ManTou mt[] = new ManTou[10];
public synchronized void push(ManTou m1){
if(index==mt.length){ //如果仓库满了话
//System.out.println(index);
try {
this.wait();//就停止线程;
} catch (InterruptedException e) {
System.out.println("仓库满了");
}
}
this.notify();//唤醒线程
mt[index] = m1;
//System.out.println(m1);
index++;
//System.out.println("index的值:" + index);
}
public synchronized ManTou pop(){
if(index==0){ //如果仓库没有东西
try {
this.wait();//停止线程,继续生产
} catch (InterruptedException e) {
System.out.println("仓库没东西");
}
}
this.notify();//线程唤醒
index--;
//System.out.println(mt[index]);
return mt[index];
}
}
class Procedus implements Runnable{
CangKu ck = null;
public Procedus(CangKu ck){
this.ck = ck;
}
public void run() {
for(int i=0;i<20;i++){
ManTou mt = new ManTou(i);
ck.push(mt);
System.out.println("生产了:" + mt);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Comer implements Runnable{
CangKu ck = null;
public Comer(CangKu ck){
this.ck = ck;
}
public void run() {
for(int i=0;i<20;i++){
ManTou mt = new ManTou(i);
ck.pop();
System.out.println("消费了:" + mt);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("仓库没东西");
}
}
}
}
public class TestRunnable {
public static void main(String[] args) {
CangKu ck = new CangKu();
Procedus p1 = new Procedus(ck);
Comer c = new Comer(ck);
new Thread(p1).start();
new Thread(c).start();
}
}
class ManTou{
private int i;
public ManTou(int i) {
this.i = i;
}
public String toString(){
return "馒头--" + i;
}
}
class CangKu{
int index = 0;
ManTou mt[] = new ManTou[10];
public synchronized void push(ManTou m1){
if(index==mt.length){ //如果仓库满了话
//System.out.println(index);
try {
this.wait();//就停止线程;
} catch (InterruptedException e) {
System.out.println("仓库满了");
}
}
this.notify();//唤醒线程
mt[index] = m1;
//System.out.println(m1);
index++;
//System.out.println("index的值:" + index);
}
public synchronized ManTou pop(){
if(index==0){ //如果仓库没有东西
try {
this.wait();//停止线程,继续生产
} catch (InterruptedException e) {
System.out.println("仓库没东西");
}
}
this.notify();//线程唤醒
index--;
//System.out.println(mt[index]);
return mt[index];
}
}
class Procedus implements Runnable{
CangKu ck = null;
public Procedus(CangKu ck){
this.ck = ck;
}
public void run() {
for(int i=0;i<20;i++){
ManTou mt = new ManTou(i);
ck.push(mt);
System.out.println("生产了:" + mt);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Comer implements Runnable{
CangKu ck = null;
public Comer(CangKu ck){
this.ck = ck;
}
public void run() {
for(int i=0;i<20;i++){
ManTou mt = new ManTou(i);
ck.pop();
System.out.println("消费了:" + mt);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("仓库没东西");
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询