编写一个程序, 四个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1,要求使用内部类

/**
 * @author laishengfeng
 * @2014-8-27
 * @TODO 编写一个程序,  四个线程,其中两个线程每次对j增加1,
 * 另外两个线程对j每次减少1(要求使用内部类线程)
 */
public class Test
{
  public static void main(String[] args) {
  MyThread mt = new MyThread();  //MyThread对象
  mt.new InnerThread1().start();
  mt.new InnerThread1().start();
  mt.new InnerThread2().start();
  mt.new InnerThread2().start();
  }

}
class MyThread 
{
  private int j = 100;
  /*对j加的内部类*/
  public class InnerThread1 extends Thread
  {
   public void run() {
   while (j < 120)
   {
    try {
     Thread.sleep(500);
    }catch (Exception ex) {
    }
    j++;
    System.out.println(Thread.currentThread().getName()+"  "+j);
   }
   }
  }
  /*对j减的内部类*/
   public class InnerThread2 extends Thread
  {
   public void run() {
   while (j > 80)
   {
    try {
     Thread.sleep(300);
    }catch (Exception ex) {
    }
    j--;
    System.out.println(Thread.currentThread().getName()+"  "+j);
   }
   }
  }
}

直接上代码  有兴趣的MM我

时间: 2024-07-28 12:35:03

编写一个程序, 四个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1,要求使用内部类的相关文章

编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果

编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果 import javax.swing.JOptionPane; public class Test{ public static void main(String[] args) { int n1=Integer.parseInt(JOptionPane.showInputDialog("Input number 1: ")); int n2=Integer.parseInt(JOptionPane.showInpu

编写一个程序找出100~999之间所有的水仙花数

如果一个3位数等于其各位的立方和,称该数为水仙花数. 如,所以407是一个水仙花数,编写一个程序找出100~999之间所有的水仙花数. 1 #include<stdio.h> 2 #include<stdlib.h> 3 //判断水仙花数,是则返回1 4 int isNarcissus(int n); 5 6 int main() 7 { 8 int i; 9 for(i = 100; i < 1000; i++) 10 if(isNarcissus(i)) 11 print

编写一个程序实现strcpy函数的功能

1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 5 6 char *mycpy(char *s1, char *s2) 7 { 8 //数组型 9 /* int i; 10 while(s2[i] != '\0') { 11 s1[i] = s2[i]; 12 i++; 13 } 14 s1[i] = '\0'; 15 return s1; */ 16 //指针型 17 char *p = s1; 18 whil

编写一个程序实现strcmp函数的功能

写自己的strcat函数------→mycmp 1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 5 int mycmp(char *s1, char *s2) 6 { 7 //数组型 8 /* int i = 0; 9 while(s1[i] == s2[i] && s1[i] != '\0') { 10 i++; 11 } 12 13 return s1[i] - s2[i]; */ 14 /

c语言:编写一个程序,可以直接接收键盘字符

编写一个程序,可以直接接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出. 程序1: #include <stdio.h> int main() { int t = 0; printf("请输入一个字符:"); t = getchar(); if (t >= 'a'&&t <= 'z') { putchar(t-32); } else if(t >= 'A'&&t 

编写一个程序,从标准输入读取几行输入。每行输入都要打印到标准输出上,前面加上行号。

编写一个程序,从标准输入读取几行输入.每行输入都要打印到标准输出上,前面加上行号. 在编写这个程序的时候要使这个程序能够处理的输入行的长度没有限制. #include <stdio.h> #include <stdlib.h> int main() { char ch = '0'; int n = 1; int flag = 1; while (1) { printf("please input the line: "); do { scanf("%c

c语言:编写一个程序,输入a,b,c三个值,输出其中最大者

程序: //编写一个程序,输入a,b,c三个值,输出其中最大者 #include<stdio.h> int main() { int a,b,c,max; printf("请输入三个数:"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max<b) { max=b; } if (max<c) { max=c; } printf("%d\n",max); retur

编写一个程序实现strcat函数的功能

写自己的strcat函数------→mycat 1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 5 char *mycat(char *s1, char *s2) 6 { 7 //数组型 8 /* int i = 0; 9 while(s1[i] != '\0') { 10 i++; 11 } 12 int j = 0; 13 while(s2[j] != '\0') { 14 s1[i] = s2[j]; 1

课后作业2——编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。

1.题目:编写一个程序,此程序从命令行接收多个数字,求和之后输出结果. 2.程序设计思想:由于命令行的参数均为字符串类型,所以要将字符串类型转换为整型,然后再进行数字的简单加和.求和运用for循环实现. 3.源程序://从命令行接收多个数字,求和之后输出结果.//20153291  冯怡晨public class SumResult {    public static void main(String[] args) { int sum=0; System.out.println("数字:&q