⑴ 在java中如何實現較為精確的定時器
大部分人在遇到需要使用定時任務的時候首先會想到Timer類,
不過在JDK5.0之後就不建議使用這個Timer了,因為它有很多的缺陷。
在新的java.util.concurrent包中的ScheledExecutorService可以替代這個Timer:
使用方法舉例:
Java代碼
ScheledThreadPoolExecutor exec = new ScheledThreadPoolExecutor(1);
exec.scheleAtFixedRate(new Runnable() {
public void run() {
try{
throw new RuntimeException();
}catch (Exception e){
System.out.println("RuntimeException catched");
}
}
}, 1000, 5000, TimeUnit.MILLISECONDS);
⑵ JAVA中如何獲取毫秒和微秒數
一、獲取毫秒數的代碼:
微秒使用System.nanoTime()方法:如果Java程序需要高精度的計時,如1毫秒或者更小,使用System.nanoTime()方法,可以滿足需求。
(2)java高精度擴展閱讀:
獲取微秒函數System.nanoTime() 的隱患:
System.currentTimeMillis() 起始時間是基於 1970.1.1 0:00:00 這個確定的時間的,而System.nanoTime()是基於cpu核心的時鍾周期來計時,它的開始時間是不確定的。
但是在多核處理器上,由於每個核心的開始時間不確定,那麼
「long start = System.nanoTime();String ip = Utilities.getIpByUrl(url);long cost = System.nanoTime() - start;」
這段代碼有可能會運行在兩個不同的cpu核心上,從而導致得到的結果完全不符邏輯。
⑶ java寫一個高精度計算器程序。
- -存儲數據的變數類型 換下就好了
c語言下的代碼 差不多的
#include<stdio.h>
#include<string.h>
char s[100001];
int a[100001],b[100001],c[100001];
int main(){
long i,j,k,m,n;
long alen,blen,clen;
gets(s);
alen=strlen(s);
for(i=alen-1;i>=0;i--)
a[alen-i]=s[i]-'0';
gets(s);
blen=strlen(s);
for(i=blen-1;i>=0;i--)
b[blen-i]=s[i]-'0';
if(alen>blen)
clen=alen;
else
clen=blen;
for(i=1;i<=clen;i++)
c[i]=a[i]+b[i];
for(i=1;i<=clen;i++)
if(c[i]>9){
c[i+1]++;
c[i]-=10;
if(i==clen)
clen++;
}
for(i=clen;i>=1;i--)
printf("%d",c[i]);
printf("\n");
return 0;
}
⑷ JAVA計算時實現數字計算結果高精度
這個程序滿足你的要求吧
public class BigNumber {
public static int[] add(int[] a, int[] b) {
int carry = 0;
int[] c = new int[a.length];
for(int i = a.length - 1; i >= 0; i--) {
c[i] = a[i] + b[i] + carry;
if(c[i] < 10000)
carry = 0;
else { // 進位
c[i] = c[i] - 10000;
carry = 1;
}
}
return c;
}
public static int[] sub(int[] a, int[] b) {
int borrow = 0;
int[] c = new int[a.length];
for(int i = a.length - 1; i >= 0; i--) {
c[i] = a[i] - b[i] - borrow;
if(c[i] >= 0)
borrow = 0;
else { // 借位
c[i] = c[i] + 10000;
borrow = 1;
}
}
return c;
}
public static int[] mul(int[] a, int b) { // b 為乘數
int carry = 0;
int[] c = new int[a.length];
for(int i = a.length - 1; i >=0; i--) {
int tmp = a[i] * b + carry;
c[i] = tmp % 10000;
carry = tmp / 10000;
}
return c;
}
public static int[] div(int[] a, int b) { // b 為除數
int remain = 0;
int[] c = new int[a.length];
for(int i = 0; i < a.length; i++) {
int tmp = a[i] + remain;
c[i] = tmp / b;
remain = (tmp % b) * 10000;
}
return c;
}
public static void main(String[] args) {
int[] a = {1234, 5678, 9910, 1923, 1124};
int[] b = {1234, 5678, 9910, 1923, 1124};
int[] c = BigNumber.add(a, b);
for(int i = 0; i < c.length; i++) {
System.out.print(c[i]);
}
System.out.println();
}
}
⑸ java怎麼處理高精度 算 10
有個類叫BigInteger,還有BigDecimal,可以解決這個問題。不過不推薦用java解決高精度計算問題 /** * 你的採納是我繼續為大家解疑答惑的東西,如解決了你的問題,望接納! */
⑹ Java中在賦值運算中可以將高精度的數據轉換為低精度嗎
可以強制類型轉換,但會丟失精度
⑺ 在java 中怎麼理解高精度和低精度
高精度和低精度 就是針對小數位數 來說的,
高精度 存儲和保留的小數位數多,經度就高。
低經度 存儲和保留的小數位數相比高精度少 ,經度低。
⑻ java能精確到小數點的後多少位
java中double類型是雙精度浮點數,佔用8位元組(Byte)即64位(bit),其精度是由32個bit的二進制尾數來確定的,因此准確精度是二進制精度而不是十進制精度,通常可以保證十進制小數點後15位有效精度和第16位的部分精度。其實這個不只是java存在,是由計算機二級制架構決定的。高精度浮點計算,最好是先轉換為整數計算後再轉為小數。相對精度比較好。
⑼ java 高精開方
做是做出來了 大半夜的 寫的有點亂 效率很低 不知道能不能優化 僅供參考吧
import java.math.BigDecimal;
public class SqrtTest
{
public static void main(String[] args)
{
Double n=2.0;
BigDecimal num=new BigDecimal(n);
BigDecimal bef;
BigDecimal aft;
for(int i=0;;i++)
{
if(i*i>n)
{
bef=new BigDecimal(i-1);
aft=new BigDecimal(i);
break;
}
}
BigDecimal res=null;
BigDecimal resup=new BigDecimal(0);
BigDecimal temp=new BigDecimal(2);
while(true)
{
res=bef.add(aft).divide(temp);
String r1=resup.toString();
if(res.multiply(res).compareTo(num)>0)
{
aft=res;
}else
{
bef=res;
}
String r2=res.toString();
int count=0;
int start=r1.indexOf('.')+1;
resup=res.multiply(new BigDecimal(1));
while(count<r1.length()&&r1.charAt(count)==r2.charAt(count))
count++;
if(count==1000+start)//1000為保留位數
{
System.out.println(" ---"+res.toString().substring(0,count));
break;
}
System.out.println(res.toString());
}
}
}
計算器 1.
程序:
---1.82152128229518488472
⑽ java:寫個Appeton高精度乘法
import java.util.*;
import java.lang.*;
import java.math.*;
import java.awt.*;
import java.awt.event.*;
public class ncifang
{
public static void main(String args[])
{
new AppFrame();
}
}
class AppFrame extends Frame
{
TextField injishu=new TextField(12);
TextField incishu=new TextField(3);
String strcifang=" ^ ";
Button btn=new Button("求n次方");
Label out=new Label("結果 ");
Label out1=new Label("n=");
Label out2=new Label("次方");
public AppFrame()
{
setLayout( new FlowLayout() );
add( injishu );
add( out1 );
add( incishu );
add( out2 );
add( btn );
add( out );
btn.addActionListener( new BtnActionAdapter() );
setSize( 1500,100 );
show();
}
class BtnActionAdapter implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
BigDecimal doublejishu;
String strResult;
String getAstring;
getAstring = injishu.getText();
doublejishu = BigDecimal.valueOf( Double.parseDouble( getAstring ) );
getAstring = incishu.getText();
int intcishu =Integer.parseInt( getAstring );
strResult = doublejishu.pow( intcishu ).toPlainString();
out.setText( "=" + strResult );
}
}
}