Asynchronous java method - java

I have a main class in which I am calling java method with #Asynchronous annotation on it. I have imported javaeeAPI6.jar for it but the method runs synchronously. Please tell me what is not right. I have added a long for loop to check whether the code goes into next statement past the asynch asyncrounousCall() call but it hangs on it
import javax.ejb.Asynchronous;
public class TestClass
{
public static void main (String[] args)
{
System.out.println("synch");
createAsyncrounousDocument("", "");
System.out.println("synch");
}
#Asynchronous
public static void asyncrounousCall()
{
for(int i = 0; i < 9999999999L ; i++)
i = i;
System.out.println("asynch");
}
}
Thank you
Aiden

Related

Java code - how to remove only annotated methods with script

I wonder if there is a way (a gradle script or any script or any other way without an IDE) to remove methods annotated with certain annotations. Example:
class x {
public static void main(String[] args) {
int x = getValue();
System.out.println(x);
}
#RemoveEnabled(id = "getValueMethod1", return = "10")
int getValue() {
return 20;
}
}
Now when I run the script or gradle target, it should remove the getValue() method, and the output code should become:
class x {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
}
Is there an existing script or way to achieve this? It might be achievable with grep and String parsing etc., but I'm looking for a cleaner solution which is able to get all methods by an annotation id, and remove them with formatting. I tried searching on Google, Stack Overflow etc., but couldn't find a solution.
I wrote a module to process similiar task.
https://github.com/KnIfER/Metaline
#StripMethods(keys={"ToRemove","AlsoRemove"})
public class YourCLZ{
void ToRemove(){} // will be removed
void AlsoRemove(){} // will be removed
#StripMethods(strip=true)
void AnotherTest(){} // will also be removed
}
in your case
#StripMethods(keys="getValue")
class yourx {
public static void main(String[] args) {
int x = getValue();
System.out.println(x);
}
int getValue() {
return 20;
}
}
will become:
class yourx {
public static void main(String[] args) {
System.out.println(x);
}
}
you will get a compilation error since for now my module simply remove any methods or method body statements that contain the key you specified.

How do I call a method that has a variable passed through it?

I'm trying to call a method from within another method. I'm understanding this simply enough, until one of those methods needs a variable carried through, and then nothing I try works.
I know that I could do this in one method, but my coursework needs me to lay it out in such a way. Why doesn't this work?
public class test2 {
public static void testMethod() {
int randomNumber = 1;
}
public static void anotherTestMethod(int randomNumber) {
System.out.println(randomNumber);
}
public static void main(String[] args) {
anotherTestMethod();
}
}
You are calling a method that has an int parameter in its signature. You should pass that parameter when calling the method. I think you are trying to use a global variable, in that case, you should declare it outside any method, as a part of the class.
public class test2 {
public static int testMethod() {
int randomNumber = 1;
return randomNumber;
}
public static void anotherTestMethod() {
System.out.println(testMethod());
}
public static void main(String[] args) {
anotherTestMethod();
}
}

Different IDEs running identical code differently

I have the following code:
public class main {
public static void main(String[] args) {
Thready howdy = new Thready();
Thread howYouDo = new Thread(howdy);
howYouDo.start();
}
}
class Thready implements Runnable{
int hi = 1;
public Thready(){}
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println(++hi);
}
}
}
It runs fine in IntelliJ regardless of the #Override annotation. In Eclipse, it forces you to remove the #Override annotation, and it works. In NetBeans, it warns you to add an #Override annotation, and the code runs, but the THREAD IS NEVER STARTED in NetBeans no matter what. Meaning the console does not print out anything.
What is going on?

JUnit: setUp and tearDown methods not working as expected

I was trying to develop some code using TDD, but stumbled on a weird behavior: setUp and tearDown doesn't seem to be "cleaning up" after each test is executed. I expected each test (marked with #Test annotation) to be executed at a random order, one after the other, without influencing each other. With that in mind, I don't understand what is happening as it seems like one specific test (testaSomarMao) is influencing another specific test (testaSplit). The testaSplit test is failling on the first assert: I expected value 6, but I'm getting 9. Anybody can explain me what is going on?
JogadorTest.java
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class JogadorTest{
private static Jogador p1;
public JogadorTest(){
}
#Before
public void setUp(){
p1 = new Jogador();
}
#After
public void tearDown(){
p1 = null;
}
#Test
public void testaGetNome(){
assertEquals(null, p1.getNome());
}
#Test
public void testaGetPontos(){
assertEquals(0, p1.getPontos());
}
#Test
public void testaSetNome(){
p1.setNome("Lucas");
assertEquals("Lucas", p1.getNome());
}
#Test
public void testaSomarMao(){
p1.comprarCarta(1);
assertEquals(3, p1.somarMao(1));
}
#Test
public void testaSplit(){
p1.comprarCarta(1);
p1.comprarCarta(1);
assertEquals(6, p1.somarMao(1));
p1.split();
assertEquals(p1.somarMao(1), p1.somarMao(2));
}
}
Jogador.java
public class Jogador {
private static String nome;
private int pontos;
private static int mao[] = new int[13];
private static int mao2[] = new int[13];
public void parar() {
}
public void setNome(String novoNome){
nome = novoNome;
}
public static int getPontos() {
return 0;
}
public static void split() {
//Usamos mao2 para garantir que soh ocorra um split.
if(mao[0] == mao[1] && mao2[0] == 0){
mao2[0] = mao[1];
mao[1] = 0;
}
}
public void fecharJogo() {
}
public String getNome() {
return nome;
}
public static int somarMao(int maoEscolhida){
int soma = 0;
int cartasNaMao[];
if(maoEscolhida == 2)
cartasNaMao = mao2;
else
cartasNaMao = mao;
for(int i = 0; i < cartasNaMao.length; i++)
soma += cartasNaMao[i];
return soma;
}
public static void comprarCarta(int maoEscolhida){
int carta = 3; // random futuramente
int cartasNaMao[];
if(maoEscolhida == 2)
cartasNaMao = mao2;
else
cartasNaMao = mao;
for(int i = 0; i < cartasNaMao.length; i++){
if(cartasNaMao[i] == 0) {
cartasNaMao[i] = carta;
break;
}
}
}
}
The underlying reason for your problem is, as pointed out by Thevenin, the use of static variables for mao[] and mao2[]
You will also need to remove static from methods comprarCarta and somarMao. You will not need it after all variables referenced inside are not static anymore. From the test it is obvious you are not using them in the way static methods are invoked usually, i.e Jogador.comprarCarta(1) instead of p1.comprarCarta(1).
My guess is you created those methods static and compiler then complained that your variables are not static and cannot be accessed, so you changed the variables as well.
Actually the use of static will cause you issues with other parts of your program as well - you better change nome variable to not be static. Same applies for getPontos() method - if you try to return pontos there instead of 0 you will get a compiler error that pontos is not static.
As you are confusing the use static in general take a look at this great explanation of what is static, when you have the time :)
Static variables - what are they?
In comprarCarta() you are modifying mao or mao2 which are static arrays. Any changes you make to these arrays will persist across all instances of Jogador.
Since this is not what you expect I suspect that you may not want these to be static.

Newbie Java Programmer needs help involving for loops

I am just trying to create an example program to help me remember how to operate for loops, when I ran it through the compiler. The Compiler said missing return statement. Where do I add it?
Here is the code:
public class LoopExample {
public String bam() {
for (int i = 0; i < 8; i++) {
System.out.println(i);
}
}
}
EDIT
I received an answer, but now the main says 'cannot find symbol'... here is the code for the main:
public class LoopExampleTestDrive {
public static void main(String[] args) {
bam looper = new bam();
System.out.println(looper);
}
}
I would advise you to try to understand how Object Oriented languages work first.
That being said, the main reason why your code doesn't work, is because you try to make an object of the class bam with new bam(). This class unfortunately doesn't exist as it is only a method in a class. My solution would look like:
public class LoopExample {
public void bam() {
for (int i = 0; i < 8; i++) {
System.out.println(i);
}
}
public static void main(String[] args) {
new LoopExample().bam();
}
}
As I said: try to understand object oriented programming first, before trying to continue programming in Java. It is too essential to be able to write working code.
PS: just to be complete, the best way to write what you want to do, would look as follows.
public class LoopExample {
public static void main(String[] args) {
for(int i = 0; i < 8; i++) {
System.out.println(i);
}
}
}
Change the signature of your method. Replace public String bam() with public void bam(). voidmeans that you return nothing instead of a String as before.
For further information see http://www.tutorialspoint.com/java/java_methods.htm

Categories

Resources