成都网站建设设计

将想法与焦点和您一起共享

多道程序运行Java代码,什么叫多道程序

JAVA程序设计,多线程,求大神给一份可运行的代码

给你一个经典的例子。run里面放空循环来观察多线程是不合理的,空循环消耗时序极小,用sleep来间隔时间才是合理的。

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

class RunnableDemo implements Runnable {

private Thread t;

private String threadName;

RunnableDemo( String name) {

threadName = name;

System.out.println("Creating " +  threadName );

}

public void run() {

System.out.println("Running " +  threadName );

try {

for(int i = 4; i  0; i--) {

System.out.println("Thread: " + threadName + ", " + i);

// Let the thread sleep for a while.

Thread.sleep(50);

}

}catch (InterruptedException e) {

System.out.println("Thread " +  threadName + " interrupted.");

}

System.out.println("Thread " +  threadName + " exiting.");

}

public void start () {

System.out.println("Starting " +  threadName );

if (t == null) {

t = new Thread (this, threadName);

t.start ();

}

}

}

public class TestThread {

public static void main(String args[]) {

RunnableDemo R1 = new RunnableDemo( "Thread-1");

R1.start();

RunnableDemo R2 = new RunnableDemo( "Thread-2");

R2.start();

}   

}

java如何实现一次编译到处运行

JVM是一个java虚拟机,所谓的java虚拟机就是和普通的虚拟机一样,拥有自己的CPU,RAM之类的。我们所使用的操作系统是Windows的操作系统,Windows操作系统支持的可执行文件是EXE文件,也就是说在Windows的操作系统上只有EXE的文件是可以直接被操作系统解释为底层机器语言并进行运行的。而java虚拟机可以支持的是.class的可执行文件,在java的虚拟机中遇到.class的文件就可以直接翻译成java虚拟机所能是别的底层机器语言并进行执行。这就是JVM的机制,正是因为java的这种机制才实现了java的跨平台,在不同的平台上安装能够在相应平台上运行的虚拟机,然后在java虚拟机中运行java的源程序,“一次编译,多次执行”就此实现了。

所以java的跨平台是离不开虚拟机的支持的。虚拟机充当着java源程序和操作系统之间的中间,不同的操作系统只需要寻找相应的中介就可以实现在不同的操作系统上运行。而java的编译只是吧.java文件编译为.class字节码文件而已,然后把字节码交给虚拟机去执行。

虚拟机在执行的时候是读一句字节码文件人后解释一句给操作系统听,这就是为什么java是解释型的语言。

所谓的编译型的语言是指这种语言被编译之后生成的是可以直接供操作系统执行的010101文件,像C,C++都是编译型的,java因为中间有JVM这么个东西所以是解释型的。

急求:多道程序设计源代码(C++或java语言可编译)?

自己复制粘贴吧。。。

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class ArtDesignFrame extends JFrame{

public ArtDesignFrame(){

setSize(400, 400);

setTitle("draw");

add(new ArtDesignPanel());

setVisible(true);

}

public static void main(String[] args) {

new ArtDesignFrame();

}

}

class ArtDesignPanel extends JPanel{

public void paint(Graphics g){

Graphics2D g2 = (Graphics2D)g;

Ellipse2D.Float ellipse = new Ellipse2D.Float(-80, 5, 160, 10);

Random random = new Random();

g2.translate(160, 90);

int R = random.nextInt(256);

int G = random.nextInt(256);

int B = random.nextInt(256);

Color color = new Color(R, G, B);

g2.setColor(color);

g2.draw(ellipse);

int i = 0;

while(i 100){

R = random.nextInt(256);

G = random.nextInt(256);

B = random.nextInt(256);

color = new Color(R, G, B);

g2.setColor(color);

g2.rotate(10);

g2.draw(ellipse);

i++;

}

}

}

*****************************************

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Label;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class Calculate extends Frame implements ActionListener{

TextField t1 = new TextField(5);

TextField t2 = new TextField(5);

TextField t3 = new TextField(5);

TextField t4 = new TextField(5);

Label l1 = new Label("=");

Button btn = new Button("计算");

public Calculate(){

setLayout(new FlowLayout());

add(t1);add(t2);add(t3);add(t4);add(l1);add(btn);

t3.addActionListener(this);

btn.addActionListener(this);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

dispose();

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e) {

float x, y;

double result = 0;

String op;

try{

x = Float.parseFloat(t1.getText());

//y = Float.parseFloat(t3.getText());

y = Float.valueOf(t3.getText());

op = t2.getText();

if(op.equals("+")) result = x + y;

else if(op.equals("-")) result = x - y;

else if(op.equals("*")) result = x * y;

else if(op.equals("/")) result = x / y;

t4.setText(Double.toString(result));

}catch(Exception ee){

t4.setText("wrong data");

}

}

public static void main(String[] args){

Calculate mainFrame = new Calculate();

mainFrame.setSize(400, 400);

mainFrame.setTitle("TWO NUMBER OPERATION");

mainFrame.setVisible(true);

}

}

************************************************

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Date;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;

public class ClockDemo extends JFrame{

public ClockDemo(){

setSize(400,400);

this.getContentPane().setBackground(Color.blue);

this.getContentPane().add(new Clock(), "Center");

setTitle("时钟演示");

setVisible(true);

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

public static void main(String[] args) {

JFrame.setDefaultLookAndFeelDecorated(true);

ClockDemo mainFrame = new ClockDemo();

}

}

class Clock extends JPanel{

Thread timer;

int oldh = -1, oldm = -1, hq = -1, mq = -1, sq = -1;

final double RAD = Math.PI / 180.0;

public Clock(){

super();

this.setOpaque(false);

ActionListener taskPerformer = new ActionListener(){

public void actionPerformed(ActionEvent evt){

repaint();

}

};

new Timer(1000, taskPerformer).start();

}

public void paintComponent(Graphics g){

int h, m, s, q, x1, y1, x2, y2;

int x0 = this.getWidth() / 2, y0 = this.getHeight() / 2;

int R = (x0 y0 ? y0 : x0) - 30;

Date d = new Date();

h = d.getHours();

m = d.getMinutes();

s = d.getSeconds();

g.setColor(Color.LIGHT_GRAY);

g.fillOval(x0 - R, y0 - R, 2 * R, 2 * R);

g.setColor(Color.black);

g.drawOval(x0 - R + 3, y0 - R + 3, 2 * R - 7, 2 * R - 7);

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

y1 = (int)(y0 - Math.cos(i * 30 * RAD) * R * (float) 4 / 5);

x1 = (int)(x0 + Math.sin(i * 30 * RAD) * R * (float) 4 / 5);

y2 = (int)(y0 - Math.cos(i * 30 * RAD) * (R - 3));

x2 = (int)(x0 + Math.sin(i * 30 * RAD) * (R - 3));

g.drawLine(x1, y1, x2, y2);

}

q = h * 30 + m / 2;

drawHand(g, x0, y0, 10, R * 4 / 7, q, Color.blue);

q = m * 6 + s / 10;

drawHand(g, x0, y0, 5, R * 2 / 3, q, Color.yellow);

q = s * 6;

drawHand(g, x0, y0, 2, R * 4 / 5, q, Color.red);

g.setColor(Color.black);

g.drawOval(x0 - 2, y0 - 2, 4, 4);

}

void drawHand(Graphics g, int x0, int y0, int w, int L, int q, Color c){

int[] x = new int[4];

int[] y = new int[4];

x[0] = x0 - (int)(Math.sin(RAD * q) * L / 6);

y[0] = y0 + (int)(Math.cos(RAD * q) * L / 6);

x[1] = x0 + (int)(Math.sin(RAD * (90 - q)) * w);

y[1] = y0 + (int)(Math.cos(RAD * (90 - 1)) * w);

x[2] = x0 + (int)(Math.sin(RAD * q) * L);

y[2] = y0 - (int)(Math.cos(RAD * q) * L);

x[3] = x0 - (int)(Math.sin(RAD * (90 - q)) * w);

y[3] = y0 - (int)(Math.cos(RAD * (90 - q)) * w);

g.setColor(c);

g.fillPolygon(x, y, 4);

}

}


本文题目:多道程序运行Java代码,什么叫多道程序
分享链接:http://chengdu.cdxwcx.cn/article/dsihpdp.html