Чтобы избежать странных побочных эффектов с закрытием в java-переменных, на которые ссылается анонимный делегат, необходимо пометить как окончательный, поэтому, чтобы ссылаться на lastPrice
и цену в задаче таймера, они должны быть отмечены как окончательные.
Это явно не сработает для вас, потому что вы хотите их изменить, в этом случае вы должны посмотреть на их инкапсуляцию внутри класса.
public class Foo {
private PriceObject priceObject;
private double lastPrice;
private double price;
public Foo(PriceObject priceObject) {
this.priceObject = priceObject;
}
public void tick() {
price = priceObject.getNextPrice(lastPrice);
lastPrice = price;
}
}
теперь просто создать новый Foo как final и вызовите .tick из таймера.
public static void main(String args[]){
int period = 2000;
int delay = 2000;
Price priceObject = new Price();
final Foo foo = new Foo(priceObject);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
foo.tick();
}
}, delay, period);
}
Спасибо DisplayName, я изменил только 2 или 3 вещи, и теперь это работает. Вот мой код, если кому-то нужен один день
Sub RecoverData()
Application.ScreenUpdating = False
Dim x As Workbook
Dim t As Range
'## Open both workbooks first:
Set x = Workbooks.Open(Application.ActiveWorkbook.Path & "\data.xlsx")
With x.Sheets("Feuil1") ' reference source sheet
Set t = .Rows(1).Find("Vendor name", lookat:=xlWhole) ' try searching wanted header in referenced sheet first row
If Not t Is Nothing Then ' if found
Range(t.Address).AutoFilter Field:=2, Criteria1:=Array("a*", "b*"), Operator:=xlFilterValues
Intersect(.UsedRange, t.EntireColumn).SpecialCells(xlCellTypeVisible).Copy _
Destination:=ThisWorkbook.Sheets("Feuil2").Range("A1") '<<== paste filtered column to destination sheet A1 cell (instead of B1), to match your subsequent removeduplicates call
ThisWorkbook.Sheets("Feuil2").Range("A1").CurrentRegion.RemoveDuplicates Columns:=1, Header:=xlYes
.AutoFilterMode = False
Else
MsgBox "Column Name Not Found"
End If
End With
x.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
вы можете попробовать это:
Sub RecoverData()
Application.ScreenUpdating = False
Dim x As Workbook
Dim t As Range
'## Open both workbooks first:
Set x = Workbooks.Open(Application.ActiveWorkbook.Path & "\data.xlsx")
With x.Sheets("Feuil1") ' reference source sheet
Set t = .Rows(1).Find("Vendor name", lookat:=xlWhole) ' try searching wanted header in referenced sheet first row
If Not t Is Nothing Then ' if found
.Rows(1).AutoFilter Field:=t.column, Criteria1:=Array("a*", "b"), Operator:=xlFilterValues ' filter data
Intersect(.UsedRange, t.EntireColumn).SpecialCells(xlCellTypeVisible).Copy _
Destination:=ThisWorkbook.Sheets("Feuil2").Range("A1") '<<== paste filtered column to destination sheet A1 cell (instead of B1), to match your subsequent removeduplicates call
ThisWorkbook.Sheets("Feuil2").Range("A1").CurrentRegion.RemoveDuplicates Columns:=1, Header:=xlYes
.AutoFilterMode = False
Else
MsgBox "Column Name Not Found"
End If
End With
x.Close
Application.ScreenUpdating = True
End Sub