62,614
社区成员
发帖
与我相关
我的任务
分享
class ThreadTest implements Runnable {
public void run() {
System.out.println ("someting run here");
}
public static void main (String[] args) {
ThreadTest tt = new ThreadTest();
Thread t1 = new Thread(tt);
Thread t2 = new Thread(tt);
t1.start();
t2.start();
//new Thread(tt).start();
}
}
public class CsdnThread {
public static void main(String []args){
Arithmetic ari=new Arithmetic(100);
Thread []ariThread=new Thread[4];
//定义加的线程
ariThread[0]=new Thread(new Plus(ari,1,50));
ariThread[1]=new Thread(new Plus(ari,1,50));
//定义减的线程
ariThread[2]=new Thread(new Plus(ari,0,10));
ariThread[3]=new Thread(new Plus(ari,0,40));
//启动线程
ariThread[0].start();
ariThread[1].start();
ariThread[2].start();
ariThread[3].start();
// 等待线程结束
for(int i=0;i<ariThread.length;i++){
try {
ariThread[i].join();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
System.out.println("运算结束,最后的结果为==》"+ari.getJ());
}
}
/**
*
* @author xiao
*
*/
class Plus implements Runnable{
Arithmetic ari=null;
int op;
int val;
/**
*
* @param ari 进行运算的对象
* @param op 运算符 1 为加,0为减
* @param val 要进行运算的值
*/
public Plus(Arithmetic ari,int op,int val){
this.ari=ari;
this.op=op;
this.val=val;
}
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println("当前的值为:"+ari.setNumber(op, val));
}
}
/**
* 进行运算的类
* @author xiao
*
*/
class Arithmetic{
private int j=0;
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public Arithmetic(int j){
this.j=j;
}
/**
* @param op 运算方式 1表示 + 0表示 -
* @param k 要改变的值
* @return
*/
public synchronized int setNumber(int op,int k){
switch (op) {
case 1:
setJ(getJ()+k);
System.out.println("执行加法 k="+k);
return getJ()+k;
case 0:
setJ(getJ()-k);
System.out.println("执行减法");
return getJ()-k;
default:
System.out.println("没有合适的计算方法");
return 0;
}
}
}
class ThreadTest extends Thread{
static int J=0;
public void run() {
System.out.println ("someting run here!");
J++;
}
public static void main (String[] args) {
ThreadTest tt = new ThreadTest();
tt.start();
}
}
public class CsdnThread {
public static void main(String []args){
Arithmetic ari=new Arithmetic(100);
Thread []ariThread=new Thread[4];
//定义加的线程
ariThread[0]=new Thread(new Plus(ari,1,50));
ariThread[1]=new Thread(new Plus(ari,1,50));
//定义减的线程
ariThread[2]=new Thread(new Plus(ari,0,10));
ariThread[3]=new Thread(new Plus(ari,0,40));
//启动线程
ariThread[0].start();
ariThread[1].start();
ariThread[2].start();
ariThread[3].start();
// 等待线程结束
for(int i=0;i<ariThread.length;i++){
try {
ariThread[i].join();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
System.out.println("运算结束,最后的结果为==》"+ari.getJ());
}
}
/**
*
* @author xiao
*
*/
class Plus implements Runnable{
Arithmetic ari=null;
int op;
int val;
/**
*
* @param ari 进行运算的对象
* @param op 运算符 1 为加,0为减
* @param val 要进行运算的值
*/
public Plus(Arithmetic ari,int op,int val){
this.ari=ari;
this.op=op;
this.val=val;
}
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println("当前的值为:"+ari.setNumber(op, val));
}
}
/**
* 进行运算的类
* @author xiao
*
*/
class Arithmetic{
private int j=0;
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public Arithmetic(int j){
this.j=j;
}
/**
* @param op 运算方式 1表示 + 0表示 -
* @param k 要改变的值
* @return
*/
public synchronized int setNumber(int op,int k){
switch (op) {
case 1:
setJ(getJ()+k);
System.out.println("执行加法 k="+k);
return getJ()+k;
case 0:
setJ(getJ()-k);
System.out.println("执行减法");
return getJ()-k;
default:
System.out.println("没有合适的计算方法");
return 0;
}
}
}